Forums Register Login

sort a List which could have Strings and integers

+Pie Number of slices to send: Send
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!

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
And welcome to the Ranch
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
What attempts have you made so far? What algorithms have you thought up so far to do it?
1
+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
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?



+Pie Number of slices to send: Send
Welcome again

Why on earth have you got a List of mixed types like that?
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2385 times.
Similar Threads
How objects of type Object downcasted to the String or other class object in the following code:
Algorithm to find the kth largest element in unsorted list
Concurrent Modification Exception: CopyOnWriteArrayList or ArrayList.Clone()
Generics query
Problem on Generic example from K&B study guide
Conceptual Misunderstanding of Arraylists?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:42:02.