Hi,
I have class of student object with the String student name and double GPA they are stored in array. My GUI have button for sort by name and sort by grade. I want to sort the name and the grade and I want to sort it using Comparator, but I don't know what is Comparator and when to use it. Also what is the diffrnet between Comparator and Comparable.
sk8ergirl
0
Newbie Poster
Recommended Answers
Jump to PostComparator is use to sort the collections. Since you have an array, u first need to convert Array into an ArrayList<Student> in generics form.
After that you need to implement Comparator interface.Please look at below code:
Person.java
package test; public class Person { private int age; …
Jump to PostHope it helps
Jump to Post[you] first need to convert Array into an ArrayList<Student>
No, he doesn't. Arrays can be sorted using
Arrays.sort
, which works just likeCollections.sort
, but for arrays.
All 9 Replies
kumar.bhatia18
0
Newbie Poster
kumar.bhatia18
0
Newbie Poster
sepp2k
378
Practically a Master Poster
kumar.bhatia18
0
Newbie Poster
sk8ergirl
0
Newbie Poster
sk8ergirl
0
Newbie Poster
sepp2k
378
Practically a Master Poster
sk8ergirl
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.