• Post Reply Bookmark Topic Watch Topic
  • New Topic

sort a List which could have Strings and integers  RSS feed

 
Neeta Bharadwaj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I want to sort a List which could have Strings AND integers and I have to keep the relative order . SO my list looks like this:

List myList = new ArrayList();
myList.add(1);
myList.add("Hey");
myList.add("Hello");
myList.add(0);
myList.add("Bye");


And the output should be 0 Bye Hello 1 Hey. I understand I cannot use the Collections.sort on this. Can any one please suggest methods or code snippets/pseudocode ? Thank you!

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the final order determined? You need to be able to explain to us how the order is determined, in English.

An example is great, but it is not a spec.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeta Bharadwaj wrote:I understand I cannot use the Collections.sort on this.

Actually you can, provided you use the one that also accepts a Comparator. So your task is to write a Comparator that can sort both Integers and Strings into the order you have specified.
Quite why you have a collection with mixed types I don't know - feels like a bad design to me. Can you explain why you are doing this.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Neeta Bharadwaj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The list could contain string and integers. The integers have to be sorted in their numerical order , strings in their alphabetical order but still their relative order should be maintained.


So if the list is 1,C,B,2,A,0

The output cannot be : 0,1,2,A,B,C

So if the list is <integer><string><string><integer><string> this order cannot change, but their values have to change

So the correct output would be : 0,A,B,1,C,2

I hope I have made the problem understandable.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What attempts have you made so far? What algorithms have you thought up so far to do it?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you need to split into two lists, sort each one separately, and then put them back together using the same pattern as the original list.

Or, as Tony suggests, use a better design that doesn't mix them up in the first place .
 
Neeta Bharadwaj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here's what I think:

For example we have LIST = (1,B,C,0,A)

We should compare every item with all the items "NEXT" in the list and swap.
Example:

var l1 = read item at List(0);
step 1: var l2= read item at List(next position);
step 2: if(DatatypeOf(l1 not equal to l2 )) goto Step1
Else
(if l1>l2 ) swap them

so first iteration we get 0,B,C,1,A

again var l1 = read item at List (1)
repeat step 2

we get 0,A,C,1,B

again var l1= read item at List(2)
repeat step 2

we get 0,A,B,1,C
What do you guys think?
Can anyone help me code this in java?



 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Why on earth have you got a List of mixed types like that?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Campbell, normally you should not have a list with unrelated types of objects (for example strings and integers).

To solve your problem, you could take an approach like the following:

First split the list into two lists: one that contains only the strings, and another one that contains only the integers.
Now sort the two lists separately (sort the list of strings and sort the list of integers).
Finally, build the result as follows: loop through the original list; when you find a string, then add the next string of the sorted list of strings to the result list; when you find an integer, then add the next integer of the sorted list of integers to the result list.
 
Neeta Bharadwaj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic mentioned by Matthew and Jesper de Jong was extremely easy to code! I could simply use the Collections.sort(myList) on both the lists.

Thank you so much.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still haven't explained why you have a list with unrelated types in it.
As has been said, it's suggestive of a bad design. If you explain the problem you are trying to solve, someone may be able to suggest something better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!