自定义排序 Flex 数组的最佳方法?
以下是该数组可以包含的字符串(按正确的顺序):
“recipients”、“columnSelector”、“headerImageLink”、“title”、“mainText”、“text”、“bodyImage”
现在,为了对它们进行排序,我显然不想做这样的事情:
if( a == 'columnSelector' && b == 'headerImageLink' ) return -1;
else if( a == 'columnSelector' && b == 'title' ) return -1;
else if( a == 'columnSelector' && b == 'mainText' ) return -1;
所以这带来了一件有趣的事情。我知道你可以使用这样的方法来优化上面的一些内容:
if( a == 'columnSelector' || a == 'bodyImage' ) return -1;
这将得到最后两个,但最近我只是想得到社区对我过去遇到的问题的意见。所以问题是,使用相对随机的顺序(不是按字母顺序等)编写自定义排序的最佳方法是什么?
谢谢!
Here are the strings this array can contain (in the correct order):
'recipients,' 'columnSelector,' 'headerImageLink,' 'title,' 'mainText,' 'text,' 'bodyImage'
Now, to sort them, I obviously don't want to do something like this:
if( a == 'columnSelector' && b == 'headerImageLink' ) return -1;
else if( a == 'columnSelector' && b == 'title' ) return -1;
else if( a == 'columnSelector' && b == 'mainText' ) return -1;
So that brings up an interesting thing. I know you can optimize the above some using something like this:
if( a == 'columnSelector' || a == 'bodyImage' ) return -1;
This would get the last two, but lately I have just wanted community input on issues I have had in the past. So the question is, what is the best way to write a custom sort, using a relatively random order (not alphabetical, etc)?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用 Array.sort 带有定义您的自定义算法的compareFunction?
Why wouldn't you use Array.sort with a compareFunction that defines your custom algorithm?