How to sort an arraylist of objects java?

后端 未结 6 1064
失恋的感觉
失恋的感觉 2020-12-10 16:26

So I want an arraylist of objects in java.

I have object1.number and object2.number, object3.number, etc... but those objects

相关标签:
6条回答
  • 2020-12-10 17:08

    Implement your own comparer:

    Arrays.sort(yourArray, new Comparator<YourClass>() {
            @Override
            public int compare(YourClass o1, YourClass o2) {
                //compare object properties
            }
    });
    
    0 讨论(0)
  • 2020-12-10 17:11

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

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

    0 讨论(0)
  • 2020-12-10 17:15

    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).

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-10 17:20

    You need to use comparator for this purpose.

    0 讨论(0)
  • 2020-12-10 17:27

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

    Collections.sort(array, new Comparator<Class>() {
        @Override
        public int compare(Class o1, Class o2) {
            //compare object properties
        }
    });
    
    0 讨论(0)
  • 2020-12-10 17:30

    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 index parameter lets you decide where in the ArrayList to add the element.

    0 讨论(0)
提交回复
热议问题