开发者

Sorting a tuple based on one of the fields

My question is the same as the one below, but the answer is very vague and I do not understand how to go through with it. sort a List<Tuple> from hig开发者_高级运维hest to lowest If you could describe in better detail how to do this it would be greatly appreciated. Thanks


Try to run this example I made for you and think what is going on:

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

public class Tuple<R, S, T>
{

private R name;
private S data;
private T index;

public Tuple(R r, S s, T t)
{
    this.name = r;
    this.data = s;
    this.index = t;
}

public static void main(String[] args)
{
    List<Tuple<String, int[], Integer>> tuples = new ArrayList<Tuple<String, int[], Integer>>();
    // insert elements in no special order
    tuples.add(new Tuple<String, int[], Integer>("Joe", new int[] { 1 }, 2));
    tuples.add(new Tuple<String, int[], Integer>("May", new int[] { 1 }, 1));
    tuples.add(new Tuple<String, int[], Integer>("Phil", new int[] { 1 }, 3));

    Comparator<Tuple<String, int[], Integer>> comparator = new Comparator<Tuple<String, int[], Integer>>()
    {

        public int compare(Tuple<String, int[], Integer> tupleA,
                Tuple<String, int[], Integer> tupleB)
        {
            return tupleA.getIndex().compareTo(tupleB.getIndex());
        }

    };

    Collections.sort(tuples, comparator);

    for (Tuple<String, int[], Integer> tuple : tuples)
    {
        System.out.println(tuple.getName() + " -> " + tuple.getIndex());
    }

}

public R getName()
{
    return name;
}

public void setName(R name)
{
    this.name = name;
}

public S getData()
{
    return data;
}

public void setData(S data)
{
    this.data = data;
}

public T getIndex()
{
    return index;
}

public void setIndex(T index)
{
    this.index = index;
}

}


The general answer is nothing else than the answer given there. To sort a list, you need either have your list elements have a canonical ordering (i.e. implementing the Comparable interface), or have a Comparator which does the comparison for the sort algorithm. Then you can use Collections.sort to do the actual work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜