java - 如何对对象的arraylist进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10145523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:47:09  来源:igfitidea点击:

How to sort an arraylist of objects java?

javasortingobjectarraylist

提问by user1253201

So I want an arraylist of objects in java.

所以我想要一个java中的对象数组列表。

I have object1.numberand object2.number, object3.number, etc... but those objects have other properties besides number, such as name, distance, etc...

我有object1.numberobject2.numberobject3.number等等......但这些对象除了具有其他属性number,如namedistance等...

So if it was sorting a string in a arrayit would just be, put a string in a temporaland let the other string take its place... but in an araryListof objects, how can I do it?

因此,如果它正在对 a 中的字符串进行排序,array那么将一个字符串放入 a 中temporal并让另一个字符串取代它的位置……但是在araryList对象中,我该怎么做?

Can I just move objects to that position of the array?

我可以将对象移动到阵列的那个位置吗?

Thanks.

谢谢。

回答by juergen d

Implement your own comparer:

实现你自己的比较器:

Arrays.sort(yourArray, new Comparator<YourClass>() {
        @Override
        public int compare(YourClass o1, YourClass o2) {
            //compare object properties
        }
});

回答by krystan honour

You need to implement the comparable interface

您需要实现可比较的接口

implements Comparable

implements Comparable

the method that does the work is

完成这项工作的方法是

public int compareTo(Object obj)
{
}

Please note that object is often replaced by a full on type because of generic syntax which can be used in the implements statement (shown below).

请注意,由于可在实现语句(如下所示)中使用的通用语法,对象通常被完整类型替换。

A full example is here in the tutorial docshope this helps

教程文档中提供了一个完整示例希望对您有所帮助

A full example (take from the above link is as follows), I have added this just in case the link goes dead at some point

一个完整的例子(取自上面的链接如下),我添加了这个以防链接在某个时候失效

import java.util.*;

public class Name implements Comparable<Name> {
    private final String firstName, lastName;

    public Name(String firstName, String lastName) {
        if (firstName == null || lastName == null)
            throw new NullPointerException();
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String firstName() { return firstName; }
    public String lastName()  { return lastName;  }

    public boolean equals(Object o) {
        if (o == null || !(o instanceof Name))
            return false;
        Name n = (Name) o;
        return n.firstName.equals(firstName) && n.lastName.equals(lastName);
    }

    public int hashCode() {
        return 31*firstName.hashCode() + lastName.hashCode();
    }

    public String toString() {
    return firstName + " " + lastName;
    }

    public int compareTo(Name n) {
        int lastCmp = lastName.compareTo(n.lastName);
        return (lastCmp != 0 ? lastCmp : firstName.compareTo(n.firstName));
    }
}

The client code from the article is:

文章中的客户端代码是:

import java.util.*;

public class NameSort {
    public static void main(String[] args) {
        Name nameArray[] = {
            new Name("John", "Smith"),
            new Name("Karl", "Ng"),
            new Name("Jeff", "Smith"),
            new Name("Tom", "Rich")
        };

        List<Name> names = Arrays.asList(nameArray);
        Collections.sort(names);
        System.out.println(names);
    }
}

回答by James Montagne

Based on your question, I take it that you are supposed to be implementing the sorting algorithm yourself. If that is the case, you can manipulate the position of elements within an ArrayList, it just works a bit differently than a regular array. Have a look at the add(int index, E element). The indexparameter lets you decide where in the ArrayList to add the element.

根据您的问题,我认为您应该自己实现排序算法。如果是这种情况,您可以操作 ArrayList 中元素的位置,它的工作方式与常规数组略有不同。看看add(int index, E element)。该index参数可让您决定在 ArrayList 中的何处添加元素。

回答by Shehzad

You need to use comparator for this purpose.

为此,您需要使用比较器。

回答by Colin D

To manipulate the entries of an ArrayList like that of a traditional array, use ArrayList.set(int, E).

要像处理传统数组那样操作 ArrayList 的条目,请使用 ArrayList.set(int, E)。

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#set%28int,%20E%29

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html#set%28int,%20E%29

回答by PAINKILLER

Use to Collections.sort()to sort an ArrayList in Java 8:

用于Collections.sort()在 Java 8 中对 ArrayList 进行排序:

Collections.sort(array, new Comparator<Class>() {
    @Override
    public int compare(Class o1, Class o2) {
        //compare object properties
    }
});