Sorting a List of LIst by the Value of the sublist

99封情书 提交于 2019-12-19 10:26:22

问题


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?


回答1:


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).compareTo(o2.get(1));
  }
})

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




回答2:


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();
  }

}



回答3:


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;
    }

}



回答4:


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.



来源:https://stackoverflow.com/questions/1080030/sorting-a-list-of-list-by-the-value-of-the-sublist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!