Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,

somebody answer me on how to sort array based on other array? i have an array which have the same index: hope it is possible. :-)

Example:
arrFirst[5]

0 = 0001\r\nThe testing1
1 = 0002\r\nThe testing2
2 = 0003\r\nThe testing3
3 = 0004\r\nThe testing4
4 = 0005\r\nthe testing5

arrSecond[5]
0 = 0002\r\nThe try1
1 = 0004\r\nThe try2
2 = 0005\r\nThe try3
3 = 0003\r\nThe try4
4 = 0001\r\nthe try5

i want to sort the arrSecond based on the sequence of arrFirst. My reference of sorting is the value before \r\n. meaning it is the number:
example:

expected output after sorting:

arrSecond[5]
0 = 0001\r\nThe try5
1 = 0002\r\nThe try1
2 = 0003\r\nThe try4
3 = 0004\r\nThe try2
4 = 0005\r\nthe try3

Thanks,
Chaegie
Posted
Updated 24-Jan-10 22:43pm
v2

IT seems to me that you want to sort based on the number at the start, not based on anything to do with the other array. You'd have to write a custom sort function that grabs the number from the substring at the front, although it does seem to me that even the default sort will sort this, even if it does it as a string sort. In fact, overall, I don't see what you're asking at all. You want to sort based on the numbers at the start of the string, but even a text based sort should do that, so where is the issue ? Is this the real data ? Please edit your post if you have a question based on real data that makes the problem more difficult than the data you posted.
 
Share this answer
 
It seems that you just need to sort the arrSecond to solve ur task.

Please split the elements using \r\n and get the first part into an array(based on any logic you want). Then sort.

I hope you will get the desired output.
:)
 
Share this answer
 
It can be done, of course. But why do you use such a convoluted way to represent your data (i.e. I strongly suggest you to change such a schema)?
:)
 
Share this answer
 
i got it! Instead of resorting the arrSecond, i will just validate if the value before the \r\n is the same with arrFirst, if true, then i will just add it in a separate string. do this until the end of the for loop. Then split again by \r\n... Thanks everyone for replying my post..

Warm regards,
Chaegie
 
Share this answer
 
Hi Christian,

Thanks for the reply.
In my application, i need to read two kinds of file. i.e (test1.txt and test1.xml) the value that you have seen before the \r\n is the page number. Now i want to merge that data based on the page number. The test1.xml having unsorted page number based on \r\n. So i need to split both data by using \r\n for me to process easier.

Thanks ,
Chaegie
 
Share this answer
 
v2

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