• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hierarchy sorting help  RSS feed

 
Vinicius Carvalho
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there! I've two classes:

Product implements Item;
Category implements Item;

Product has a category. And category is a self-reference class, having a parent class. (Typical kind of category)

My question is. I must have an ADT (Abstract Data Structure) which holds my categories and products in a special order. For example:

If I insert a product it goes to the root. then I add a parent category to that product so my sort becomes:
C1 -> P1
Now let's suppose I add a category that has no relation with both:
C1 -> P1 -> C2
Now let's add the mother of em all:
CX -> C1 -> P1 -> C2
A product that belongs to C1 and it's name is less than P1
CX -> C1 -> P2 -> P1 -> C2

and so on...

Any ideas of how could I do this?

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd be tempted to make an ordered list of C and an ordered list of P for each C.

Each C needs to have a name and a list of P. If you look through the collections in Java you'll find a nice way to associate a name with some object. you should also find a nice way to keep an ordered list of P. Try this introduction to collections:

http://java.sun.com/j2se/1.5.0/docs/guide/collections/index.html

Sorry if this seems overly vague, but I'm betting you'll have much more fun finding the details for yourself than just having us rattle them off. Let us know what you find there that looks interesting.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!