基于其他数组的度假村数组 [英] Resort array based on other array

查看:80
本文介绍了基于其他数组的度假村数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人回答我如何基于其他数组排序数组?我有一个具有相同索引的数组:希望有可能. :-)

示例:
arrFirst [5]

0 = 0001 \ r \ n测试1
1 = 0002 \ r \ n测试2
2 = 0003 \ r \ n测试3
3 = 0004 \ r \ n测试4
4 = 0005 \ r \ n测试5

arrSecond [5]
0 = 0002 \ r \ ntry1
1 = 0004 \ r \ ntry2
2 = 0005 \ r \ ntry3
3 = 0003 \ r \ ntry4
4 = 0001 \ r \ ntry5

我想根据arrFirst的顺序对arrSecond进行排序.我的排序参考是\ r \ n之前的值.意思是数字:
例如:

排序后的预期输出:

arrSecond [5]
0 = 0001 \ r \ ntry5
1 = 0002 \ r \ ntry1
2 = 0003 \ r \ ntry4
3 = 0004 \ r \ ntry2
4 = 0005 \ r \ ntry3

谢谢,
Chaegie



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

推荐答案

在我看来,您想基于开头的数字进行排序,而不是基于与其他数组有关的任何排序.您必须编写一个自定义排序函数,该函数从前面的子字符串中获取数字,尽管在我看来,即使是默认排序也可以对它进行排序,即使它是作为字符串排序也是如此.实际上,总的来说,我根本看不到你在问什么.您想根据字符串开头的数字进行排序,但是即使是基于文本的排序也应该这样做,那么问题出在哪里呢?这是真实数据吗?如果您有一个基于实际数据的问题,这会使问题比您发布的数据困难,请编辑您的帖子.
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.


似乎您只需要对arrSecond进行排序即可解决您的任务.

请使用\ r \ n拆分元素,然后将第一部分放入数组(根据您想要的任何逻辑).然后排序.

希望您能获得所需的输出.
:)
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.
:)


当然可以做到.但是,为什么要使用这种复杂的方式表示数据(即,我强烈建议您更改这种模式)?
:)
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)?
:)


这篇关于基于其他数组的度假村数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆