Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am stuck at the part of Sorting a group of integers according to their length and Un-Sorting it back.

Example
Below is my integer array that i want to get serialized
int[] i = new int[] { 20,5,90,7,44,81 };

OUTPUT
i = new int[] { 5,7,20,90,44,81 };

UNSORTING
i = new int[] { 20,5,90,7,44,81 };

So far, i cannot find a way to unsort the list back. Any Code snippet would be appreciated.


Thanks in advance
Posted
Updated 14-Mar-19 14:42pm
v2
Comments
Philippe Mori 22-Nov-14 13:16pm    
This si not possible. As simple as it. You have to remember original ordering to be able to restore it. Both solutions 1 and 2 are good solutions for your problem.

Sorting is not reversible. Unsorting does not exists. Well in some case, you might get the original order from the original data for example if the data come from a database you might make the original query again and you would get the original order without explicitly rememebering the order (provided the data do not change in the mean time).

Quote:
unsort


Don't alter the array -- make another array with the indices of the first array and alter that -- it's kind of a linked list that way.

int[] i = new int[] { 20,5,90,7,44,81 };

int[] j = new int[] { 1 , 3 , 0 , 2 , 4 , 5 };
 
Share this answer
 
Comments
agent_kruger 22-Nov-14 1:44am    
no sir i cant save it in another object and ain't there any way to unsort the list?
Philippe Mori 22-Nov-14 13:13pm    
You cannot unsort an array without somehow remember original order somehow. Sorting is not a reversible operation.
Try this

C#
int[] i = new int[] { 20, 5, 90, 7, 44, 81 };
           int[] unsortedarray = new int[i.Length];
           i.CopyTo(unsortedarray, 0);
           Array.Sort(i);  // sorted array  5,7,20,44,81,90
           i = unsortedarray; //un sorted array  20, 5, 90, 7, 44, 81
 
Share this answer
 
v2
Comments
agent_kruger 22-Nov-14 1:44am    
no sir i cant save it in another object and ain't there any way to unsort the list?
Karthik_Mahalingam 22-Nov-14 2:39am    
share your code..
agent_kruger 22-Nov-14 3:04am    
i dont have a code of unsorting that is what i am looking for
Karthik_Mahalingam 22-Nov-14 3:26am    
check my updated solution...
Philippe Mori 22-Nov-14 13:13pm    
You cannot unsort an array without somehow remember original order somehow. Sorting is not a reversible operation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900