• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting in List<Hash Map>

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All ,
I i have the data in below format:

Name Age Skill Company
Vass 21 Java Zylog
Samy 24 PB HP
Lee 18 ADF CTS
Reng 16 Java Info


I converted this data into java collections List<Hash Map> like this.





As per data (table format) i want to sort the data in Column level
how can i to achieve ?.

List<HashMap> is type of collection is help to me?

Any idea?

Thanks,
Vass Lee
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vass Lee wrote:As per data (table format) i want to sort the data in Column level

List<HashMap> is type of collection is help to me?


You'd be much better off creating a class to hold that data (Person?) and then just creating a List<Person>. If you make your Person class Comparable, you can just sort it as is; otherwise one of the Collections.sort() methods also takes a Comparator.

Winston
 
Vass Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Winston,

Thanks for your reply. Can you provide any sample for Creating Person class and how to hold the data in that class.?

I am new guy in java

Thanks,
Vass Lee,
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as winston suggested, make a Person class like below

and create a list of type Person and add objects to it like this


you can change the compareTo() method according to your need
hope this may help you.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nomaan Butt wrote:make a Person class like below


@Vass: the only slight change I'd make to Nomaan's suggestion is not to use Strings for everything. 'Age' is plainly a numeric value, so make it one, viz:and I suspect you'll also discover that something like "skill" is actually a code that is stored elsewhere in your database. If it is, then make it a class (or an enum) too. If not, forget what I said and just use a String.

HIH

Winston
 
Vass Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Winston,

Thanks for your reply.Now i want to apply some filters in column level .how can i apply filter in person class? any filter method is there in java.util. package? can you provide any sample here ?

Thanks,
Vass Lee
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now i want to apply some filters in column level .how can i apply filter in person class?


What do you mean by column level? Nomaan's code shows how to sort based on the variable name.... You want to do any more addition to the compareTo() method?
 
Vass Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Thanks for your reply.As per Nomaan's i can able to sort the variable level.Now i want filter the records in variable level.

Thanks,
Vass Lee
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Vass - i am not understanding what do you mean by filtering...

Might be you want to iterate the List<Person> and take a particular person object if the name variable has a particular value (say "Vass"?)
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a java.util.Comparator<Person> implementation for all the specific ways you want to compare the Person objects.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vass Lee wrote:Thanks for your reply.Now i want to apply some filters in column level .how can i apply filter in person class? any filter method is there in java.util. package? can you provide any sample here ?


Just a small point: you're in Java now and you have a class, so it's best not to think in terms of "columns" any more. Databases have columns, Java has fields (or attributes). Assuming that, per Nomaan's example, you have a java.util.List of Persons, a filtering method might look something like this:however, filters and sorts being what they are, there are endless possibilities (and you might even want to combine the two), so as the others say you should probably look at java.lang.Comparable and java.util.Comparator as well.

Also, Java has a dizzying array of collection types, some of which are very powerful, so I suggest you have a look at the tutorials and get familiar with them.

HIH

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic