0

Question

How can i sort a array of object based on a specific value? In my case the "leeftijd" (age) value?

CODE

Klant[] klantarray = new Klant[2];
klantarray[0] = new Klant("Brian ", "van den ", "Heuvel", + 
        21, true, "Krimpen aan den IJssel", "brian@me.com");
klantarray[1] = new Klant("piet", "","mak", 96, true, "Rotterdam",+ 
        "pietje@rdam.com");

I need to sort it on the values 96 and 21, where 21 comes on top of the (new) list. and 96 at the bottom.
There will be more objectes added in due time.

i want to use the Mergesort but i have no idea how to use it

user3671459
  • 455
  • 3
  • 8
  • 21

1 Answers1

2

You can use the sort method from Arrays class using a Comparator.

vz0
  • 32,345
  • 7
  • 44
  • 77
  • i would like to use a Merge-sort if possible. – user3671459 Nov 25 '14 at 16:26
  • @user3671459 Do you need to implement the sorting yourself? If not, then the sort method is already a mergesort; the sort documentation says: Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. – vz0 Nov 25 '14 at 16:31
  • Yes i do need to implement the sorting myself, although it is not required, i would like to understand the way of how this is done – user3671459 Nov 25 '14 at 19:21
  • @user3671459 My advice for you is to create a new question where this requirement, implementing merge sort in Java code, is clearly stated. Otherwise the usual answer would be my answer, which is to use the sort call bundled with Java. – vz0 Nov 25 '14 at 20:20
  • thank you, will do! you've been most helpfull – user3671459 Nov 25 '14 at 20:21