0
public static void main(String args[]) {
   int g=0;int x=0;int y=0;
    List<Integer> temporaryOpenList = new ArrayList<>();
    List<ArrayList> listToSort = new ArrayList<>();
    for(int i=3 ; i>0 ; i--) {
        g = g + i;
        x = x + i;
        y = y + i;
        temporaryOpenList.add(g);
        temporaryOpenList.add(x);
        temporaryOpenList.add(y);
        ArrayList<Integer> openList = new ArrayList<Integer>(temporaryOpenList);
        temporaryOpenList.clear();
        listToSort.add(openList);
        System.out.println(listToSort);
   }

The output will be :

[[3, 3, 3]]  
[[3, 3, 3], [5, 5, 5]]  
[[3, 3, 3], [5, 5, 5], [6, 6, 6]]

Now I want to sort listToSort based upon g value means the 1st value of the value triplet [g,x,y].But collections.sort(listToSort) command doesn't work

4
  • 3-5-6 This is already sorted nope ? Commented Feb 14, 2018 at 17:55
  • I'm sorry, but where did you get the idea that standard Java sorting supports sorting list of lists based on a first value of a triplet??? Commented Feb 14, 2018 at 17:55
  • 1
    I suggest revising your title to focus on your task (sorting triplets within list) rather than complaining about Collections.sort. Commented Feb 14, 2018 at 18:33
  • Thank You all of you for your attention. I got it. Commented Feb 15, 2018 at 1:45

2 Answers 2

2

You have to provide your own Comparator:

Collections.sort(list1, (elem1, elem2) -> {
    return elem1.get(0).compareTo(elem2.get(0));
});

To make this compilable you have to change the declaration of list1 to

ArrayList<ArrayList<Integer>> list1 = new ArrayList<>();

I wrote that directly into the text editor in my browser, so if you find a typo, you can keep it ;-)

Sign up to request clarification or add additional context in comments.

Comments

2

Try to use a clean comparator to sort your elements:

import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;
import java.util.Collections;

public class MyClass {
public static void main(String args[]) {
   int g=0;int x=0;int y=0;
    List<Integer> temporaryOpenList = new ArrayList<>();
    List<List<Integer>> listToSort = new ArrayList<>();
    for(int i=3 ; i>0 ; i--) {
        g = g + i + 8; // i added 8 just for change 'g' value, to use it for sort
        x = x + i;
        y = y + i;
        temporaryOpenList.add(g);
        temporaryOpenList.add(x);
        temporaryOpenList.add(y);
        ArrayList<Integer> openList = new ArrayList<Integer>(temporaryOpenList);
        temporaryOpenList.clear();
        listToSort.add(openList);

   }

   Collections.sort(listToSort, new MyComparator());
   System.out.println(listToSort);
}
}




class MyComparator
    implements Comparator<List<Integer>>
{
    public int compare(List<Integer> list1, List<Integer> list2) {
        return list1.get(0).compareTo( list2.get(0));
    }
}

That works! but you should know that comparators are some of good things to use when you try to sort or compare objects.

5 Comments

Thank you . I subtract 3 from g for better understanding. It show that sorting is in ascending order. If I want to sort it in descending order what will I changed in MyComparator class. Also this Comparator function is new to me. I don't understand the code.Can you suggest me a link other than the Java documentation from where I can learn it.
If you want to sort it in descending order you only need to change code to return list2.get(0).compareTo( list1.get(0)); . For more details about Java Comparator you can see this
@Nomade alternatively just negate the result of compareTo, i.e. return -list1.get(0).compareTo( list2.get(0));
@Lothar, yes it is an other solution to get such result, but i would prefer to do it clean by using a function instead of - operator : return negate(list1.get(0).compareTo( list2.get(0))); Where negate is : public int negate(value) { return (value = -value); }
@Nomade If clarity is your goal you can use Collections.sort(listToSort, Collections.reverseOrder(new MyComparator()));

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.