This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Unable to sort single column when we have multiple column using Java comparator  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have below format of data

A                        B
Tamil            English
Bangalore     Karnataka
Go's                 Read
Fisha              Basics

But I need the below output

A            B
Bangalore     English
Fish's             Karnataka
Go's                 Read
Tamil              Basics



First A column changed like asc or desc then  the B column remain sorted and same vice versa

I used comparstor but thats not helpful.
 
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We'd need to see your code to make any sense of it. What data structure are you using to hold columns A and B?
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList < HashMap < String, Object>> contactList;

A                        B
Tamil            English
Bangalore     Karnataka
Go's                 Read
Fisha              Basics

Collections.sort(contactList, new Comparator<Map<String, Object>>() {
       @Override
       public int compare(Map<String, Object> o1, Map<String, Object> o2) {
           if(null != o1.get("status") && null != o1.get("status")){
               return o2.get("status").toString().compareTo(o1.get("status").toString());
           }else if(null != o1.get("status")){
               return 1;
           }else{
               return -1;
           }
       }
   });
 
Carey Brown
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the additional info but I'm still guessing at some of the blanks.

You have a List of Maps that you are trying to sort. The map represents columns by the use of String keys. So a key of "status" might represent one column. You want to sort on this "status" column while NOT sorting any other column. This is a contradiction and can't be supported by the structure as you've laid out. By sorting on any column you impact the order of all other columns.

You have
Why are you converting them to a String instead of
Are all your columns Strings? If so then you'd have a Map<String,String> not Object.
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes got my point. When unsorted particular column all the column sorted.
No it's like hashmap<string, Object>
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore the previous reply.

Yes you are absolute correct. When I try sort single column but all the other column also sorted

We have the data like
List<Hashmap<String, Object>
 
Carey Brown
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Baktha Elumalai wrote:No it's like hashmap<string, Object>

So why use toString() then to perform the comparison?
 
Carey Brown
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I've said, you can't meet your sorting requirements with your current data structure. If you were to convert your data to
then you could sort the List for key "status" without affecting any other List.
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.

Could you please explain the with sample will understand seaily.
 
Carey Brown
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How soon convert hashmap<String, Object> into map<String , List<Object>>

Thanks in advance
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not possible to put data into list of object why because lot functionality working already.
Without put data into list of object any other way is there any possibility

Thanks I advance.
 
Master Rancher
Posts: 3036
106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Baktha Elumalai wrote:How soon convert hashmap<String, Object> into map<String , List<Object>>

Thanks in advance


It is possible, but since the keys in a HashMap are unique, the List<Object> would only contain one element: the Object. So, it wouldn't make much sense.

Question: why do you have, in your opening post, an ArrayList<HashMap>? Wouldn't just a HashMap be sufficient?

Lastly, if you have a Map<String, Object> map and you want to sort the keys, burt not the values, then it is relatively simple. For instance, to sort the keys, you could do:

Can you create from this set and the values of the map a new HashMap?
 
Carey Brown
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We've offered solutions based on the  sketchy requirements that you've provided. Then you say your current code works according to other parts of the requirements that you haven't shown us. I think that in order to proceed that you need to share with us the complete requirements and your complete code.
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<Quote>
[{Da=1Box, PROOF=N, Var=No, id=AG-012},
{Da=1Box, PROOF=N, Var=No, id=AI001},
{Da=1Box5, PROOF=N, Var=No, id=DSB-01},
{Da=1Box3, PROOF=N, Var=No, id=DSB-02},
{Da=1Box9, PROOF=N, Var=No, id=Demo2},
{Da=1Box3, PROOF=N, Var=No, id=MI2-001},
{Da=2Boxes24, PROOF=N, Var=Yes, id=MI3-001},
{Da=1Box1, PROOF=N, Var=No, id=TESTSTD001},
{Da=1Box2, PROOF=N, Var=Yes, id=ZI001}]
<Quote>

from the above  list<Map<string, Object>>arraylist=Arraylist()..

i need to sort the value only id other no need to sort

what am going to do
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Baktha Elumalai wrote:<Quote>
[{Da=1Box, PROOF=N, Var=No, id=AG-012},
{Da=1Box, PROOF=N, Var=No, id=AI001},
{Da=1Box5, PROOF=N, Var=No, id=DSB-01},
{Da=1Box3, PROOF=N, Var=No, id=DSB-02},
{Da=1Box9, PROOF=N, Var=No, id=Demo2},
{Da=1Box3, PROOF=N, Var=No, id=MI2-001},
{Da=2Boxes24, PROOF=N, Var=Yes, id=MI3-001},
{Da=1Box1, PROOF=N, Var=No, id=TESTSTD001},
{Da=1Box2, PROOF=N, Var=Yes, id=ZI001}]
<Quote>

from the above  list<Map<string, Object>>arraylist= new Arraylist()..

i need to sort the value only id other no need to sort

what am going to do

 
Marshal
Posts: 62172
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a new question, or a continuation of your previous question?
 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
continuation with previous question
 
Carey Brown
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorting by column 'id', which of these scenarios are you trying to achieve?

 
Baktha Elumalai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I want to try to do Option A
 
Carey Brown
Bartender
Posts: 5283
55
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those problems where creating an SSCCE really helps. It is very difficult to solve if it's embedded in a large program. This is also one of those problems that is easy to do mentally but requires thought on how to break down the steps in order to be able to write a program to execute those steps. As a human, your first step is to isolate a single column's worth of data. The next step is to sort this data. And the last step is to re-insert the data back into the original table. These are exactly the same steps that you code will need to perform. And because the implementation is just a bit tricky, creating an SSCCE to write it and test it is pretty much required. An SSCCE can also help us to see where you may have gone wrong and permits us to reproduce the problem.

Here's an example:
 
Always look on the bright side of life. At least this ad is really tiny:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!