简体   繁体   English

排序类数组java

[英]sorting class array java

I have made a class as follows: 我做了一个课程如下:

class strVal{
    double val;
    String str;
}

Now I made an array of this class now I want to sort that array by strVal.val . 现在我创建了这个类的数组,现在我想通过strVal.val对该数组进行strVal.val I want to know if there is any standard defined method in Java? 我想知道Java中是否有任何标准的定义方法?

Implement java.lang.Comparable interface, and use java.util.Arrays.sort(...) to sort an array of StrVal: 实现java.lang.Comparable接口,并使用java.util.Arrays.sort(...)对StrVal数组进行排序:

public class StrVal implements Comparable<StrVal> {

  private double val;
  private String str;

  public StrVal(double val, String str) {
    this.val = val;
    this.str = str;
  }

  @Override
  public int compareTo(StrVal o) {
    return (int)(this.val - o.val);
  }

  public double getVal() {
    return val;
  }

  public String getStr() {
    return str;
  }
}

To sort the array : 要对数组进行排序:

StrVal[] array;

...

Arrays.sort(array)

Collections.sort might help you. Collections.sort可能会帮助你。 Use a List<strVal> and then: 使用List<strVal> ,然后:

Collections.sort(list, comparator);

Some more code: 更多代码:

List<strVal> list = ...;
Collections.sort(list, new Comparator<strVal>()
                        {
                            @Override
                            public int compare(strVal o1, strVal o2)
                            {
                                return o1.val - o2.val;
                            }
                        });

Or if you don't want to use a List, use Arrays.sort(array, comparator) . 或者,如果您不想使用List,请使用Arrays.sort(array, comparator)

You need to implement Comparator or Comparable interface and then call Collections.sort on the collection of obejects of your class strVal. 您需要实现ComparatorComparable接口,然后在类strVal的obejects 集合上调用Collections.sort Follow this tutorial for learning more about collections sorting: 请按照本教程了解有关集合排序的更多信息:

http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

now I made an array of this class now I want to sort that array by strVal.val. 现在我创建了这个类的数组 ,现在我想通过strVal.val对该数组进行排序。

You can implement a Comparator , override its compare() method and use Arrays.sort() . 您可以实现Comparator ,覆盖其compare()方法并使用Arrays.sort()

In case you use a List<strVal> use Collections.sort() . 如果您使用List<strVal>使用Collections.sort()

If you think that the comparison of the class objects can be done only by its val property and that property defines the natural ordering of the objects then your class can implement Comparable . 如果您认为类对象的比较只能通过其val属性来完成,并且该属性定义了对象的自然顺序,那么您的类可以实现Comparable

Comparator : 比较器:

new Comparator<strVal>()
{
     @Override
     public int compare(strVal o1, strVal o2)
     {
          return o1.val - o2.val;
     }
});

Comparable: 可比:

class strVal implements Comparable<strVal>{
    double val;
    String str;

    @Override
    public int compareTo(strVal o) {
         return this.val - o.val;
    }   
}

Ps: Please adhere to Java naming conventions. Ps:请遵守Java命名约定。

You will have to implement the comparable interface for that and make your val field the field where the compare will take place. 您必须为此实现类似的接口,并使您的val字段成为进行比较的字段。

More info here: http://www.javapractices.com/topic/TopicAction.do?Id=10 更多信息: http//www.javapractices.com/topic/TopicAction.do?Id=10

您需要实现Comparable -interface并使用集合的排序方法

You should use the sort method defined in the Arrays utility class . 您应该使用Arrays实用程序类中定义的sort方法。

In order to sort according to your key you should either make your class implement Comparable or provide a Comparator . 为了根据您的密钥进行排序,您应该使您的类实现Comparable或提供Comparator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM