0

I've created a list in java as following :

1   bbbb london
1.1 aaaa nyc
1.10 cccc jaipur
...
1.2 test test
1.11 test1 test1

I need to sort it based on index 1,1.1,1.2 etc! These are string values.

Like:

1   bbbb london
1.1 aaaa nyc
1.2 test test
...
1.10 cccc jaipur
1.11 test1 test1

How can i do that?

Initially the index was float but in-order to get 1.10 in list i changed index to string so Collection.sort(list) doesnt give output as expected.

My purpose is like to create a numbered bullets like

1 Helo
 1.1 helo1
 1.2 helo2
 1.3 hello3
2 Test
 2.1 Test1
 2.2 Test2
3 World

Any help please?

5
  • I recommend having separate fields for the major and minor indices. Commented Dec 23, 2018 at 6:59
  • @NicholasK . It can only be 1 - 1.1 - 1.2 ... 1.10 - 1.11 - 2 - 2.1 - 2.2 blah blah. Its like adding intermediate points between two main points. Like numbered bullets Commented Dec 23, 2018 at 7:03
  • @TimBiegeleisen. But if i create another indices with float value the 1.10 will change to 1.1 and their will be two 1.1 and sorting will get wrong right? Commented Dec 23, 2018 at 7:05
  • You might have a working answer below. From a data model point of view, it is generally bad to store numbers as strings. Commented Dec 23, 2018 at 7:12
  • Check out this answer: stackoverflow.com/questions/44617214/… Commented Dec 23, 2018 at 7:13

2 Answers 2

5

Given the list :

List<String> list = Arrays.asList("1.32   bbbb london", "21.1 aaaa nyc", 
                                  "100.10 cccc jaipur", "1.2 test test",
                                  "1.11 test1 test1");

You can write your own custom comparator as shown below :

Collections.sort(list, new Comparator<String>() {
    public int compare(String o1, String o2) {
        return Float.compare(Float.parseFloat(o1.split(" ")[0]),
                             Float.parseFloat(o2.split(" ")[0]));
    }
});

Here, we split the String by " " and fetch the floating part of the string which happens to be the first index of the array. Now we parse it to a float and compare it with the second string.

If you are on java-8 you could do it one line :

Collections.sort(list, (o1, o2) -> 
      Float.compare(Float.parseFloat(o1.split(" ")[0]), Float.parseFloat(o2.split(" ")[0])));
Sign up to request clarification or add additional context in comments.

8 Comments

Collections.sort(mRoute.getWayPointList(), new Comparator<WayPoint>() { public int compare(WayPoint o1, WayPoint o2) { return Float.compare(Float.parseFloat(o1.getId().split(" ")[0]), Float.parseFloat(o2.getId().split(" ")[0])); } }); Actualy i tried this still no it is not sorting! elements in List is added through a model Waypoint! I Could get the index(id) using that model.getId
Is the String stored in the field id? If possible do share the class Waypoint
This answer does not work. Yet it has 4 votes. My answer works and it has -1 votes. It's funny :)
@PrasadKarunagoda : This solution does work. Please go ahead and try it. As mentioned in the comments by OP, probably he is not using it correctly as there is a model class Waypoint. That's the reason I've asked him to share the file if possible. Meanwhile you could try out the answer for yourself.
@NicholasK, I tried your code on the input data provided in the question. And it does not give the answer expected in the question. I think the point you are missing is, Dillz's requirement is not for a "natural" ordering. That's why it does not work as String nor float.
|
-1

Try this. This should work. Main logic is in compareTo method.

import java.util.ArrayList;
import java.util.Collections;

public class Sort
{
  public static void main(String[] args)
  {
    ArrayList<Entry> entries = new ArrayList<>();
    entries.add(new Entry("1", "bbbb london"));
    entries.add(new Entry("1.1", "aaaa nyc"));
    entries.add(new Entry("1.10", "cccc jaipur"));
    entries.add(new Entry("1.2", "test test"));
    entries.add(new Entry("1.11", "test1 test1"));

    Collections.sort(entries);
    for (Entry e : entries)
    {
      System.out.println(e);
    }
  }
}

class Entry implements Comparable<Entry>
{
  String index;
  String value;
  int major;
  int minor;

  Entry(String index, String value)
  {
    this.index = index;
    this.value = value;

    String[] array = index.split("\\.");
    if (array.length == 2)
    {
      major = Integer.valueOf(array[0]);
      minor = Integer.valueOf(array[1]);
    }
    else if (array.length == 1)
    {
      major = Integer.valueOf(array[0]);
    }
    else
    {
      throw new IllegalArgumentException("Invalid index : " + index);
    }
  }

  @Override
  public int compareTo(Entry otherEntry)
  {
    if (this.major < otherEntry.major)
    {
      return -1;
    }
    else if (this.major > otherEntry.major)
    {
      return 1;
    }
    else
    {
      if (this.minor < otherEntry.minor)
      {
        return -1;
      }
      else if (this.minor > otherEntry.minor)
      {
        return 1;
      }
      else
      {
        return 0;
      }
    }
  }

  @Override
  public String toString()
  {
    return index + " " + value;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.