Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

sorting - How to sort an arraylist of objects java?

So I want an arraylist of objects in java.

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

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?

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

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Implement your own comparer:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...