0

I need help to find the better way to sort my array and show it on the screen. Here is the complex object:

Category category;
Article article;
BigDecimal price;

And I want to show an Array of this object on this way:

Category
->Article
  ->Price
->Article
  ->Price
  ->Price
->Article
  ->Price
Category
->Article
  ->Price
  ->Price
->Article
  ->Price

So, heres a example of array (Category and Article objects have Id's, but im gonna use name so its gonna be more easy to understand):

{"Kitchen", "Fork", 5}
{"Kitchen", "Fork", 8}
{"Kitchen", "Knife", 5}
{"Kitchen", "Spoon", 3}
{"Bathroom", "Soap", 5}
{"Bathroom", "Shampoo", 3}
{"Bathroom", "Shampoo", 7}

So, I want to show it like this:

Kitchen
->Fork
  ->5
  ->8
->Knife
  ->5
->Spoon
  ->3
Bathroom
->Soap
  ->5
->Shampoo
  ->3
  ->7

I know, im not posting any code, but its because I dont know how to do it. Maybe making a lot of loops, but im here for suggestions..

Sorry for my bad english, I hope you understand!

Julián
  • 101
  • 1
  • 12
  • use comparator interface from java.util package – koutuk Dec 02 '16 at 06:16
  • complex object can be sorted if you implement `Comparable` interface, [read this for more example](http://stackoverflow.com/questions/2839137/how-to-use-comparator-in-java-to-sort). As for **show an array of complex object** i strongly suggest you `override` the `toString()` method that inherited from `Object` class. – Bagus Tesa Dec 02 '16 at 06:16

0 Answers0