#How to Sort an array list of objects alphabetically according to one of their attributes (Java)

17 messages · Page 1 of 1 (latest)

worldly sapphire
#

I have a movie class which is derived from a media class. I made an arraylist of movie objects called "movies" and want to be able to sort the arraylist according to one of the movie's attributes -title of movie(sort by name of movie alphabetically essentially). To do get the names of each Movie I have done the below, but now I want to create a newarraylist that is sorted by the name of the movie alphabetically.

// Get name of each individual movie
System.out.println(movies);
length=movies.size();
for (int i=0; i<length;i++){
Movie tempMov = (Movie) movies.get(i);
System.out.println(tempMov.getName());
}
Things I want to know/understand how to do: -How can I sort an arraylist alphabetically (and numerically alphabetically for watchtime as a seperate sort) according to the attributes of an object?

code below should explain most of what I have done

I have tried googling but due to being new to Java I am quite overwhelmed. Any help would be appreciated! The imgur link demonstrates the code I have got (only the important parts). Thanks!

tulip auroraBOT
#

This post has been reserved for your question.

Hey @worldly sapphire! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

patent cobalt
#

god my eyes

#

use List.sort or Collections.sort to sort the list by providing a comparator

#

that comparator can be constructed from static methods of Comparator

#

you can use method references to construct the comparator

worldly sapphire
patent cobalt
#

no but the image is really bright but the text is small lmao

#

i can barely even read the code there

worldly sapphire
#

you can open in browser if you want

#

the image

patent cobalt
#

nah it's not necessary

worldly sapphire
patent cobalt
worldly sapphire
#

ok, I'll start there