按照java

时间:2015-06-13 17:14:16

标签: java sorting arraylist row

我有6个ArrayLists,如下所示:

index: 0         1           2          3            4          5

[12345.12   |123.0      |12.12      |1234.0     |123.12     |123.12    ] <Double>
[2000-01-11 |2000-01-11 |2000-01-11 |2000-01-12 |2000-01-12 |2000-01-11] <String>
[1234       | 1234      | 1234      | 1235      | 1235      | 1234     ] <String>
[4          | 10        | 16        | 24        | 30        | 20       ] <Integer>
[7          | 13        | 19        | 27        | 34        | 25       ] <Integer>
[9          | 15        | 21        | 29        | 35        | 40       ] <Integer>

使用Java,我想按降序排列第一个列表的值如果第一个列表中的值是等于,则按自然顺序将第二个列表中的相应值对两个相等的值进行排序。第二个列表中的字符串可以通过调用Collection.sort(second_list)按自然顺序排序。

(例如:因为索引4和5处的元素是等于的,我必须按照自然顺序在第二个列表中的索引4和5处的元素对它们进行排序:123.12 = 123.12但是&#34; 2000 -01-12&#34;&gt;&#34; 2000-01-11&#34;,所以最后两个索引的顺序为5,4)

排序后的列表应如下所示:

     0            3           5          4            1         2 

[12345.12   |1234.0     |123.12     |123.12     |123.0      |12.12     ] <Double>
[2000-01-11 |2000-01-12 |2000-01-11 |2000-01-12 |2000-01-11 |2000-01-11] <String>
[1234       | 1235      | 1234      | 1235      | 1234      | 1234     ] <String>
[4          | 24        | 20        | 30        | 10        | 16       ] <Integer>
[7          | 27        | 25        | 34        | 13        | 19       ] <Integer>
[9          | 29        | 40        | 35        | 15        | 21       ] <Integer>

我已经尝试构建一个字符串的ArrayList,其中列表中的每个元素都包含一个列中的元素,如上所示(例如:列为索引0)。在上面的列中的每个元素之后,我将字符串用逗号连接&#34;,&#34;所以我可以在排序后拆分字符串(例如:&#34; 12345.12,2000-01-11,...,9&#34;。因为我没想到它排序了,我放弃了这个计划。

我需要一种允许行中重复值的表。

也许如果第一个List是一个Map,其中索引是键,值是上面ArrayLists中的元素,我可以这样做:

  • 我按值对Map<Integer, Double>的值进行排序。 索引 - 是键,第一个列表中的元素 - 是值。所以我获得了索引的顺序:0 3 5 4 1 2
  • 我按照此自定义顺序对其他ArrayLists进行排序,但我的工作如果Map<Integer, Double>中的值是重复的?如何按照第二个List中元素的自然顺序对它们进行排序?

......我需要一个能够快速对所提到的列表进行排序的结构。

2 个答案:

答案 0 :(得分:1)

使用您的值名称创建一个项目对象:

public class Item {

    private Double value1;

    private String value2;

    private String value3;

    private Integer value4;

    private Integer value5;

    private Integer value6;

    public Double getValue1() {
        return value1;
    }

    public void setValue1(Double value1) {
        this.value1 = value1;
    }

    public String getValue2() {
        return value2;
    }

    public void setValue2(String value2) {
        this.value2 = value2;
    }

    public String getValue3() {
        return value3;
    }

    public void setValue3(String value3) {
        this.value3 = value3;
    }

    public Integer getValue4() {
        return value4;
    }

    public void setValue4(Integer value4) {
        this.value4 = value4;
    }

    public Integer getValue5() {
        return value5;
    }

    public void setValue5(Integer value5) {
        this.value5 = value5;
    }

    public Integer getValue6() {
        return value6;
    }

    public void setValue6(Integer value6) {
        this.value6 = value6;
    }        

}

在其他课堂上使用这个:

public class Test {

    ArrayList<Item> items;

    public Test(){
        items = new ArrayList<>();
        this.dataList();
    }

    public void dataList(){
        Item item = new Item();
        item.setValue1(12345.12);
        item.setValue2("2000-01-11");
        // add all your values
        items.add(item);              
        // do the same for all your objects

        //Sort your list with a custom Comparator
        Collections.sort(items, new Comparator<Item>() {
            @Override
            public int compare(Item item1, Item item2) {
                return item1.getValue1().compareTo(item2.getValue1());
            }
        });        
     }
}

答案 1 :(得分:0)

这是一种粗略的排序实现,它使用来自几个相等长度的独立可比较列表中的值来创建一个列表,其中列表代表排序键。

public static void sort( List<List<? extends Comparable>> lol ){
    List<Integer> index = new ArrayList<>();
    for( int i = 0; i < lol.get(0).size(); ++i ){
        index.add( i );
    }
    Collections.sort( index, new CompInd( lol, index) );
    for( int i: index ){
        for( int j = 0; j < lol.size(); ++j ){
            System.out.print( " " + lol.get(j).get(i) );
        }
        System.out.println();
    }
}

Class CompInd实现n个列表的索引的比较器。

class CompInd implements Comparator<Integer> {
    private List<List<? extends Comparable>> comp;
    private List<Integer> index;
    public CompInd( List<List<? extends Comparable>> comp, List<Integer> index ){
        this.comp = comp;
        this.index = index;
    }

    public int compare(Integer ind1, Integer ind2){
        for( int i = 0; i < comp.size(); ++i ){
            int res = 
                comp.get(i).get(ind1).compareTo( comp.get(i).get(ind2) );
            if( res != 0 ) return res;
        }
        return 0;
    }
}

主方法创建三个列表(来自您的数据)并调用排序。

public static void main( String[] args ){
    List<Double> dl1 = new ArrayList<>();
    for( double d: new double[]{12345.12, 123.0, 12.12, 1234.0, 123.12, 123.12} ){
        dl1.add( d );
    }
    List<String> sl1 = new ArrayList<>();
    for( String s: new String[]{"2000-01-11","2000-01-11","2000-01-11","2000-01-12","2000-01-12","2000-01-11"} ){
        sl1.add( s );
    }
    List<String> sl2 = new ArrayList<>();
    for( String s: new String[]{"1234","1234","1234","1235","1235","1234"} ){
        sl2.add( s );
    }

    List<List<? extends Comparable>> lol = new ArrayList<>();
    lol.add( dl1 );
    lol.add( sl1 );
    lol.add( sl2 );
    sort( lol );
    }
}

这适用于任意数量的并行列表,前提是它们都是实现Comparable的对象列表。

相关问题