Hey everyone,
I am having a small problem with sorting objects.
I have a method called organizeTank. It should sort the fish in the tank such that: (1) all dead fish come first sorted according to size in descending order, (2) all live fish come next such that herbivores come first sorted according to size in descending order and carnivores come next sorted according to speed in ascending order.

          {specie, size, speed, type, condition}
fish[0] = {"shark", 3, 7, "herbivore", true};
fish[1] = {"cat fish", 6, 10, "herbivore", false};
fish[2] = {"gold fish", 5, 7, "herbivore", true};
fish[3] = {"cat fish", 10, 8, "carnivore", true};
fish[4] = {"shark", 6, 10, "herbivore", true};
fish[5] = {"gold fish", 1, 4, "herbivore", false};
fish[6] = {"cat fish", 2, 6, "herbivore", false};

I will have to sort these objects according to the instructions, how do I that using selection sorting?

Thanks for your help!

Recommended Answers

All 3 Replies

You write code for selection sort!
Might help to see Comparable as well, you can make your implementation a bit generic.

you may sort that code by using Arrays.sort(your variable here);

@decade
... and how will Arrays.sort(fish); work?
how is it going to compare the elements to see in which order they should be ordered?
@htmler
I would recommend you to check on tekashyap's answer and the links he provided.

if you want to use Arrays.sort(fish), as decade suggested, you will have to implement the compareTo method of the Comparable interface, in which you can declare how your elements should be sorted.

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.