简体   繁体   English

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

[英]How to sort an arraylist of objects java?

So I want an arraylist of objects in java. 所以我想在java中使用对象的arraylist。

I have object1.number and 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 array it would just be, put a string in a temporal and let the other string take its place... but in an araryList of objects, how can I do it? 因此,如果它正在对array的字符串进行排序,那么就是将字符串放在一个temporal ,让另一个字符串取代它......但是在对象的araryList中,我该怎么做呢?

Can I just move objects to that position of the array? 我可以将对象移动到数组的那个位置吗?

Thanks. 谢谢。

Implement your own comparer: 实现自己的比较器:

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

You need to implement the comparable interface 您需要实现类似的界面

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). 请注意,对象通常由full on类型替换,因为通用语法可以在implements语句中使用(如下所示)。

A full example is here in the tutorial docs hope 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);
    }
}

您需要使用比较器来实现此目的。

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. 如果是这种情况,您可以操纵ArrayList中元素的位置,它只是与常规数组有点不同。 Have a look at the add(int index, E element) . 看看add(int index, E element) The index parameter lets you decide where in the ArrayList to add the element. index参数允许您决定在ArrayList中添加元素的位置。

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

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

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

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