Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort List<List<String>> by list value

Tags:

java

list

sorting

I want sort the List by values in the list , it contains three values , first value is integer i convert that into string and other two values are string in nature, i want to sort the list by first string .

List<List<String>> detail_View = new ArrayList<List<String>>();

List<String> fieldValues =fieldValues = new ArrayList<String>();
String aString = Integer.toString(ScreenId);
fieldValues.add(aString);
fieldValues.add(DisplayScreenName);
fieldValues.add(TableDisplay);

detail_View.add(fieldValues);

In above code i have to sort list values by ScreenId

like image 397
MohanMaverick Avatar asked Feb 15 '12 06:02

MohanMaverick


People also ask

Can you sort a list of lists in Java?

Collections class sort() method is used to sort a list in Java. We can sort a list in natural ordering where the list elements must implement Comparable interface. We can also pass a Comparator implementation to define the sorting rules.

How do I sort a string into characters?

Using the toCharArray() method Get the required string. Convert the given string to a character array using the toCharArray() method. Sort the obtained array using the sort() method of the Arrays class. Convert the sorted array to String by passing it to the constructor of the String array.

How do I sort a list in Comparator?

To sort an ArrayList using Comparator we need to override the compare() method provided by comparator interface. After rewriting the compare() method we need to call collections. sort() method like below.


2 Answers

You have to use two concept:

  1. Collections.sort utility.
  2. Comparator<T> interface.

I write your solved problem following:

First you have to write your comparator:

class CustomComparator implements Comparator<List<String>>
    {
        @Override
        public int compare(List<String> o1,
            List<String> o2)
        {
            String firstString_o1 = o1.get(0);
            String firstString_o2 = o2.get(0);
            return firstString_o1.compareTo(firstString_o2); 
        }
    }

then you using Collections utility as following:

Collections.sort(detail_View, new CustomComparator());

after these step, your list:

List<List<String>> detail_View = new ArrayList<List<String>>();

will sorted by first index of any nested list.

For more information related to this concept see:

  1. http://www.javadeveloper.co.in/java-example/java-comparator-example.html
  2. http://www.roseindia.net/java/java-tips/data/collections_non_generic/comparators.shtml
like image 108
Sam Avatar answered Sep 19 '22 06:09

Sam


Supposing that you do not want to sort the integer (stored as a String) alphabetically, you cannot use the default comparator, but have to convert it back to an integer first:

Collections.sort(detail_view, new Comparator<List<String>>(){
    int compareTo(List<String> a, List<String> b){
         return Integer.valueOf(a.get(0)).compareTo(Integer.valueOf(b.get(0));
    }
});

May I suggest not using a List for the three pieces of data, but your own bean class?

like image 39
Thilo Avatar answered Sep 20 '22 06:09

Thilo