Suppose I have data as :
Stud_Num 03, stud_roll 110,name etc
Stud_Num 03, stud_roll 108
Stud_Num 03, stud_roll 112
Stud_Num 03, stud_roll 110
Stud_Num 02, stud_roll 110
Stud_Num 01, stud_roll 110
I need the list to be sorted as
Stud_Num 01, stud_roll 110
Stud_Num 02, stud_roll 110
Stud_Num 03, stud_roll 108
Stud_Num 03, stud_roll 110
Stud_Num 03, stud_roll 110
Stud_Num 03, stud_roll 112
i.e., first by stud_Num then by stud_roll.
Thanks,
Gressh
> Thanks for you quick reply but may I know how can I
> make use of TreeMap to sort the list (list contains
> Transfer Obejects).
By reading the tutorial and applying the concepts therein to your code (note that TreeMap will maintain order by the natural ordering of the keys).
[url=http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]The Java?Tutorial - Object Ordering[/url]
[url=http://www.onjava.com/lpt/a/3286]Making Java Objects Comparable[/url]
If you've tried the techniques from the article(s) and have come up short, let us know what specific difficulty you're having. Otherwise, please review the article(s) and apply the techniques from the article(s) to your code first.
~
The most simple implementation is to sort the whole list by the first attribute, num, then sort each portion of sequential nums by roll. Meaning once you get them in order by num, sort each group of elements that has the same num value by the roll value. There are probably improvements to be made, which you can discover by googling sorting algroithms.
> > The most simple implementation...
>
> ...would be to implement Comparable or write a
> Comparator and let the existing API handle the
> sorting. No algorithm searches necessary. :o)
>
> ~
Correct me if I'm wrong, but you still have to imlpement the sorting yourself when you use Comparable, right? Meaning Comparable doesn't know automatically how to sort your objects on two fields, it just lets you automatically apply that Comparable to the list.
> you still have to imlpement the sorting yourself when
> you use Comparable, right?
Not really. You'll be defining the comparison, not the sorting.
> Meaning Comparable doesn't know
> automatically how to sort your objects on two fields,
> it just lets you automatically apply that Comparable
> to the list.
In order to implement Comparable, you'll need to define the behavior of the compareTo() method, thereby imposing a natural ordering for the class. If it doesn't make sense to have a natural ordering, or if you need more than one method of sorting, implement a Comparator.
~
Ok, but the comparison is the meat and potatoes that defines the order, and you still have to define that in terms of your attributes. So the comparison would be something like: A comes before B if (A.num < B.num and A.roll < B.roll). It's more compact this way, but the order is still defined by you. The sorting itself is done for you when you apply that comparison.
I think we're both talking about the same thing, and I know I'm splitting hairs, but I'm doing it as much for my own knowledge as for the OP's.
> I am able to sort the list by stud_num but how to
> sort it by stud_roll again as sorting by stud_roll
> would sort the list by only stud_roll and loses the
> sorting done by stud_Num.
Pseudocode:
If stud_nums are equal, compare stud_roll.
This is explained and demonstrated in the tutorial. Please let us know if you have any further questions.
~
> I think we're both talking about the same thing, and
> I know I'm splitting hairs, but I'm doing it as much
> for my own knowledge as for the OP's.
If I read this right, you were going to implement a custom sorting algorithm for this specific problem, something like
sort(List listOfWhateverObjectHasStudNumAndStudRollAsAttributes)
I think the principle of "encapsulate what varies" would be violated there. Why implement a sorting algorithm when it's already been implemented? If all you do is implement the compareTo() method, you're keeping your code slightly more maintainable.
> > I think we're both talking about the same thing,
> and
> > I know I'm splitting hairs, but I'm doing it as
> much
> > for my own knowledge as for the OP's.
>
> If I read this right, you were going to implement a
> custom sorting algorithm for this specific problem,
> something like
> sort(List
> listOfWhateverObjectHasStudNumAndStudRollAsAttributes)
>
I think the principle of "encapsulate what
> varies" would be violated there. Why implement a
> sorting algorithm when it's already been implemented?
That's not what I was suggesting. Well, I guess I kinda was in my first reply, but not after that, maybe that's where the confusion came from.
> If all you do is implement the compareTo() method,
> you're keeping your code slightly more maintainable.
That's what I was talking about when I was replying to yawmark.