0

I have an ArrayList of Johnsons' Cycles found by Using an API. Now I have to sort this ArrayList but I was stuck to do so. I am newbie in JAVA. I would be very thankful If someone please help me. The ArrayList looks like

[[Hausi Müller, Johannes Martin, Bruce Winter, J Martin], [Kenny Wong, Johannes Martin], [Kenny Wong, Kostas Kontogiannis, Johannes Martin, Bruce Winter, J Martin], [Ludger Martin, Johannes Martin, Bruce Winter, J Martin], [Arun Venkataramani, Jian Yin, J-P Martin, Michael Dahlin, J Martin], [Kenny Wong, Kostas Kontogiannis, Johannes Martin], [Kostas Kontogiannis, Kenny Wong, Johannes Martin], [Augustin Lux, Christophe Le Gal, James L Crowley], [Christophe Le Gal, Augustin Lux, James L Crowley]]

This is a part of the big list.

Rustam
  • 6,485
  • 1
  • 25
  • 25
paras
  • 49
  • 1
  • 6
  • `Collections.sort()` ? – user902383 Sep 22 '15 at 12:59
  • 1
    possible duplicate of [Sort Java Collection](http://stackoverflow.com/questions/6957631/sort-java-collection) – Vogel612 Sep 22 '15 at 13:06
  • What are the sorting conditions? What defines the order of the elements? – Nicholas Robinson Sep 22 '15 at 13:10
  • 1
    It's not `ArrayList`. It is `ArrayList>`. Isn't it? – Rustam Sep 22 '15 at 13:11
  • @Rustam It is ArrayList>. and it is in the result of I have a graph in which vertices are authors and edges are co-authorship relation. Then I call JohnsonSimpleCycles jSC = new JohnsonSimpleCycles<>(); jSC.setGraph((DirectedGraph) directedGraph); List> savedElements = jSC.findSimpleCycles(); System.out.println(cycles); That ArrayList of ArrayList is the output of this function. Now I have to sort these inner list on the bases of their number of authors. – paras Sep 22 '15 at 14:32
  • @NicholasRobinson on the number of authors in the inner list. – paras Sep 22 '15 at 14:33

3 Answers3

0

It is possible to use Collections.sort function. Basically this function take a list as an argument and sort it using a custom Comparator or the Comparable interface of the elements present in the list.

Here is the documentation to sort collections (so also ArrayList) in java.

Here an example of a custom comparator for List<String> elements (it can be used if the api returns an object of type List<List<String>>.

public CustomComparator implements Comparator<List<String>> {
    public int compare(ArrayList<String> o1, ArrayList<String> o2) {
        return o1.size() - o2.size(); 
    }
}

....

List<List<String>> myResult = ....;
Collections.sort(myResult, new CustomComparator());
// Here myResult is sorted by size of inner lists.

Note: If you like to sort on the other direction simply change compare function to return o2.size() - o1.size()

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • Collections.sort does not work. As these arraylist of arraylist are the output johnsons cycle detector. and I want to sort these cycles by the number of authors in the list. If an ArrayList has six authors and the other has five then I want to place the second list (having five authors) first and then first list (having six authors). – paras Sep 22 '15 at 14:00
  • You have to write a custom comparator. Collections.sort works without problem. Post the type of the arrayList returned by this library and I will add a custom comparator to show you how to do that. – Davide Lorenzo MARINO Sep 22 '15 at 14:01
  • Thanks. I have a graph in which vertices are authors and edges are co-authorship relation. Then I call JohnsonSimpleCycles jSC = new JohnsonSimpleCycles<>(); jSC.setGraph((DirectedGraph) directedGraph); List> savedElements = jSC.findSimpleCycles(); System.out.println(cycles); That ArrayList of ArrayList is the output of this function. Now I have to sort these inner list on the bases of their number of authors. – paras Sep 22 '15 at 14:27
0

here is an example how to sort your Objects via Collections.sort by using a comparator:

       //this example is a snippet from my actual project, 
        //but it fits your question very well I think

            Comparator<TMObject> comp = getTmObjectComparator();
            int leastTMC = Collections.min(tmObjectList, comp).getTMC();

            private Comparator<TMObject> getTmObjectComparator() {
                return new Comparator<TMObject>() {


                  @Override
                  public int compare(TMObject o1, TMObject o2) {

                    //I compare if the int TMC of Object one 
                    //is bigger than the int TMC of Object two
                    //this also works with alphabetical order

                    if (o1.getTMC() > o2.getTMC()) {

                      //returning a Number > 0 means it is superordinate
                      return 1;
                    }
                     //returning a Number < 0 means it is subordinate
                    return -1;
                  }
                };
              }

If your Object is as Simple as a String you can also compare the strings with o1.compareTo(o2). Thats quite the basic.

Tom Wellbrock
  • 2,982
  • 1
  • 13
  • 21
0

If it's an arraylist of arraylists I suggest you do

for(List<String> list: yourListOfLists){
 Collections.sort(list); }

Now, every list in your list is sorted, but how do you want to sort the list-of-lists?

pmartin8
  • 1,545
  • 1
  • 20
  • 36
  • Type mismatch: cannot convert from element type Object to List – paras Sep 22 '15 at 13:42
  • Hmm... then you have a list of Objects (can you show how you built your list?). Try Collections.sort(yourListOfLists); if it does the trick. – pmartin8 Sep 22 '15 at 13:46
  • I have a graph in which vertices are authors and edges are co-authorship relation. Then I call JohnsonSimpleCycles jSC = new JohnsonSimpleCycles<>(); jSC.setGraph((DirectedGraph) directedGraph); List> savedElements = jSC.findSimpleCycles(); System.out.println(cycles); That ArrayList of ArrayList is the output of this function. Now I have to sort these inner list on the bases of their number of authors. – paras Sep 22 '15 at 14:24
  • You print, System.out.println(cycles); but I can't see what "cycles" is... If you want to sort according to the number of authors, this is an important information that you need to include in your original question. – pmartin8 Sep 22 '15 at 14:58
  • Sorry the cycles is actually savedElements. savedElements = jSC.findSimpleCycles() this is the output and I have to sort this according to number of authors in each returned cycle of saved elements. I have printed it to see its output. Yes I want to sort according to the number of authors in each ArrayList of ArrayList. – paras Sep 23 '15 at 04:46
  • To help you building the comparator, we need to know exatcly what's in your list. A List of lists of what? Ie. List> savedElements you need to define what '?' is. – pmartin8 Sep 23 '15 at 13:37
  • In savedElements, the cycles of authors found in the Publication graph made from a list of authors is saved. This graph has vertices that represents authors and edges are co-authorship relation. Then I call JohnsonSimpleCycles jSC = new JohnsonSimpleCycles<>(); jSC.setGraph((DirectedGraph) directedGraph); List> savedElements = jSC.findSimpleCycles(); System.out.println(cycles); That ArrayList of ArrayList is the output of this function. Now I have to sort these inner list on the bases of their number of authors – paras Sep 24 '15 at 07:20