Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
imnotinsane
Jul 19, 2006
I can't seem to wrap my head around how I am meant to solve this issue, I think I am coming from the wrong angle.

If I have an class object that contains 3 values, for example Student() and it holds firstName, lastName, studentId.

I have then created an array of Student[] objects, so now i am holding something like
code:
Student[] list = new Student("John", "Smith", 123), new Student("George", "Citizen", 567), new Student("Alan", "Bates", 789);
Now if i wanted to sort them by say student id i can't really loop through the array list because all three values are held in the same object. I was thinking maybe I am meant to use a 2d array of objects, so that i have list[0][1-3] but then that would still leave me with the same problem as I am not really storing the actual value just the object it self that contains those three associated variables.

The other way I was thinking was to make a method that returned the values explicitly as array, for example, Student.getArrayValues() and I guess create a new 2d array list on the fly and then manually sort that through a loop and print the results but it feels like I'm doubling up my work for nothing and there should be a simpler method.

Any help would be appreciated

Adbot
ADBOT LOVES YOU

imnotinsane
Jul 19, 2006

ivantod posted:

Maybe this will help you: https://www.baeldung.com/java-8-comparator-comparing.

By using the Arrays.sort(...) method you can give it a lambda function telling it how to compare the objects. This allows you to compare by one (or more) chosen fields (e.g. in your case student id).

Ahh that is exactly what I was looking for, thanks

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply