views:

386

answers:

4
private List<String> subList;
private List<List<String>> records = new ArrayList<List<String>>();

for(....){

    subList = new ArrayList<String>();
    ...populate..
    records.add(subList);
}

For example, subList has three Strings - a, b, and c. I want to sort the records by the value of b in subList.

records at 0 has a list of "10", "20", "30"
records at 1 has a list of "10", "05", "30"
records at 2 has a list of "10", "35", "30"

After the sort, the order of records should be -

records at 0 = records at 1 above
records at 1 = records at 0 above
records at 2 = records at 2 above

What could be a good algorithm for that?

+4  A: 

This is just like sorting a string of characters: given two strings, start at the beginning and compare each character; if there's a difference, the string with the lower value comes first, otherwise, look at the next characters from each string. If the strings are of different lengths, treat the shorter string as if it had a suffix of zeroes.

In this case, the "characters" are integer values, obtained by calling Integer.parseInt(). Additionally, implementing a Comparator for a List<String> would be helpful here. Then the Collections.sort() method can be used.

The comparator might look something like this:

final class MyComparator implements Comparator<List<String>> {

  public int compare(List<String> a, List<String> b) {
    /* Assume all strings are parseable to values 
     * in range [0,Integer.MAX_VALUE] */
    int len = Math.min(a.size(), b.size());
    for (int idx = 0; idx < len; ++idx) {
      int va = Integer.parseInt(a.get(idx)), vb = Integer.parseInt(b.get(idx));
      if (va != vb)
        return va - vb;
    }
    return va.size() - vb.size();
  }

  @Override
  public boolean equals(Object o) {
    return o instanceof MyComparator;
  }

  @Override
  public int hashCode() {
    return MyComparator.class.hashCode();
  }

}
erickson
Depending upon the context, it might also be worth replacing `List<String>` with an encapsulating class.
Tom Hawtin - tackline
Yes, for example, an encapsulating class could prevent any strings that are added are parseable as integers in the allowable range.
erickson
+2  A: 

Something like:

Collections.sort(records, new Comparator<List<String>>()
{
  public int compare(List<String> o1, List<String> o2)
  {
    //Simple string comparison here, add more sophisticated logic if needed.
    return o1.get(1).compare(o2.get(1));
  }
})

Though I find hard-coding the positions a little dubious in practice, your opinion may differ.

Kevin Montrose
1 is the index of the "b"?
dfa
I read the question as "sort by the second element in each sublist", by index 1 in other words.
Kevin Montrose
A: 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class MyList 
{

    private List<List<Long>> myList;

    public MyList()
    {
     myList = new ArrayList<List<Long>>();
     ArrayList arrayList = null;

     for(int i=0;i<3;i++)
     {   
      arrayList = new ArrayList<Long>();
      for(int x=0;x<3;x++)
      {   
       arrayList.add((Long)Math.round(Math.random()*10));
      }
      myList.add(arrayList);
     }  
    }

    public static void main(String[] args)
    {
     MyList newList = new MyList();
     newList.printList();
     Collections.sort(newList.getMyList(),new Comparator<List<Long>>(){

      public int compare(List<Long> o1, List<Long> o2) {
       if(o1 != null && o2 !=null)
       {
        Long var1 = o1.get(0);
        Long var2 = o2.get(0);

        return var1.compareTo(var2);     
       }
       return 0;
      } 
     });
     newList.printList();
    }

    private void printList() {
     for(List<Long> subString : myList)
     {
      System.out.println("List");
      for(Long elements : subString)
      {
       System.out.println(elements);
      }
     }

    }

    public List<List<Long>> getMyList() {
     return myList;
    }

    public void setMyList(List<List<Long>> myList) {
     this.myList = myList;
    }

}
Priyank
A: 

The Column Comparator allows your to sort on any column within the List. The sort is done using the natural sort order of the data in the column.

camickr