• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to group/order/sort elements in a List?  RSS feed

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am having a List which consists of Objects of type Section. The Section object consists properties called name and image. These properties are populated from the database.

Valid names are Krishna, Karthik, Naaz, Vipul.

I want to rearrange the items on this list. The arrangement of the Objects must be like this

Naaz
Karthik
Krishna
Vipul

I think Sorting will not work. Can anyone please let me know how to do this.

Thank you all in advance, good day.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you are using List, you can sort it using the Collection interface . you need to implement the Comparator interface in your Section class.

Hope that helps.

Vishal.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Shaw wrote: . . . If you are using List, you can sort it using the Collection interface . you need to implement the Comparator interface in your Section class. . . .
Do you mean the Collections class? Do you mean Comparator or Comparable? I think you should have a look at the Java Tutorials, where there is a section about “object ordering”.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, in regular cases we opt for Comparator or Comparable. But I don't want to sort. I want to rearrange the Objects based on the string the Objects contain.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is sorting. Only you use the String as a basis for the ordering.… and woe betide you if you ever let any null values into the text field
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote:Yeah, in regular cases we opt for Comparator or Comparable. But I don't want to sort. I want to rearrange the Objects based on the string the Objects contain.

Right, so you need some sort of rule that defines HOW you want to rearrange them. What is it? Because it doesn't appear to be alphabetical.

Winston
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Right, so you need some sort of rule that defines HOW you want to rearrange them. What is it? Because it doesn't appear to be alphabetical.

Winston


Right now I am using this logic
Is this a good approach? Or is there even better approach than this?

Or I can do it at database level also. Simply by adding a column called as ArrangeOrder. This will be a numeric column. I will simply add 1, 2, 3, 4 and order the result in ascending.

Creating a column in the database is secondary idea. I just want to know at the code level.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is sorting. Only you use the String as a basis for the ordering.… and woe betide you if you ever let any null values into the text field

Yeah previously we were using this codeI don't want to sort, I want to rearrange the objects.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote: . . . Yeah previously we were using this code
… which looks exactly the same as what I suggested.
I don't want to sort, I want to rearrange the objects.
That is sorting. Only in a different order, as Winston said yesterday. You need to find some way of ordering your Strings which returns the values for your different kind of sorting from a Comparator. I don’t know what ordering you want, but you can doubtless work that out for yourself.

And if you can get your database program to do it for you, so much the better.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote:Is this a good approach? Or is there even better approach than this?

There is. Create a Section class that has a name and a number, and have it implement the Comparable (java.lang.Comparable) interface in a way that compares Sections by their number. For example:
Then your array becomes:and it doesn't matter what order you put them in, because you can always get them in sorted order with
Arrays.sort(sections);

BTW, the above is only ONE way you could do it; there are plenty of others that may be even better for your requirements.

Lesson: Java is an Object-Oriented language, so the best solutions usually involve classes.

Winston
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay you suggest me to use an index. But I don't have one. I think indexing it in the database and ordering it there is a good approach.

Thank you Winston. ::thumbs::
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote:Okay you suggest me to use an index. But I don't have one.

Then make one. There's nothing that says you have to use it anywhere else (although I suspect you'll probably find you want to), but this is your ordering requirement, and it doesn't seem to follow any pattern other than "I want this one after that one because I say so".

So: give it a number.

Winston
 
Prashant He
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to override name and number attribute in your Section class.
then you can compare the objects in the list using comparator interface..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Then make one...

And one possible way of generating an order on the fly, would be to add the following method to your Section class:and then you can simply do:
Section[] sections = Section.sections("Naaz", "Karthik", "Krishna", "Vipul");

However, yet another way of doing it, if you know what your sections are going to be in advance, would be to make them an Enum (java.lang.Enum), in which case you might want to make Section an interface.

There are MANY ways to do this, so don't just copy down the first idea you like. Think about it.

Winston
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston, I will choose the best approach.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote:Thank you Winston, I will choose the best approach.

FYI, a colleague pointed out a mistake in that last method. Now corrected.

Winston
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I will have to go with maintaining a column in the database because the order of the names are likely to change sometimes.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me how can I use an Enum in this scenario? Should I create an Enum with the name and index and then get the order from the sections(String... names) method?
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to explain a lot more about your Enum suggestion first. It doesn’t spring to mind as obviously correct, so maybe it would be a poor solution.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You would have to explain a lot more about your Enum suggestion first. It doesn’t spring to mind as obviously correct, so maybe it would be a poor solution.

Hi Ritchie, are you telling this to me or Winston?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote:Hi Ritchie, are you telling this to me or Winston?

Me, I suspect. And Campbell's quite right - it may not be the best.

However, if (and only if), your sections are always used in the same order, and they're not likely to change often, the Enum might look like:and
Section.Krishna.name()
would return "Krishna", and
Section.values()
would return you a Section[] array with all of your Sections in their defined order.

Winston

 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was telling you
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie, master of ambiguity.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Vishal Shaw wrote: . . . If you are using List, you can sort it using the Collection interface . you need to implement the Comparator interface in your Section class. . . .
Do you mean the Collections class? Do you mean Comparator or Comparable? I think you should have a look at the Java Tutorials, where there is a section about “object ordering”.


Oops... ! Sorry for my negligence, I meant Collections class
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Campbell Ritchie, master of ambiguity.
I had gone down with a bad attack of, “ask a silly question …”
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!