javascript中快速稳定的排序算法实现 [英] Fast stable sorting algorithm implementation in javascript

查看:18
本文介绍了javascript中快速稳定的排序算法实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对大约 200-300 个对象的数组进行排序,按特定键和给定顺序 (asc/desc) 排序.结果的顺序必须一致且稳定.

I'm looking to sort an array of about 200-300 objects, sorting on a specific key and a given order (asc/desc). The order of results must be consistent and stable.

最好使用的算法是什么,您能否提供一个在 javascript 中实现的示例?

What would be the best algorithm to use, and could you provide an example of it's implementation in javascript?

谢谢!

推荐答案

可以从不稳定的排序函数中得到稳定的排序.

It is possible to get a stable sorting from a non-stable sort function.

在排序之前你会得到所有元素的位置.在您的排序条件中,如果两个元素相等,则按位置排序.

Before sorting you get the position of all the elements. In your sort condition, if both elements are equal, then you sort by the position.

多田!你有一个稳定的排序.

Tada! You've got a stable sort.

如果您想了解有关此技术的更多信息以及如何实现它,我已经在我的博客上写了一篇关于它的文章:http://blog.vjeux.com/2010/javascript/javascript-sorting-table.html

I've written an article about it on my blog if you want to know more about this technique and how to implement it: http://blog.vjeux.com/2010/javascript/javascript-sorting-table.html

这篇关于javascript中快速稳定的排序算法实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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