[英]How can I sort this JavaScript object based on the name and then order by relevance?
I asked the following question earlier: Can JavaScript or jQuery sort a JSON array on multiple criteria? 我之前曾问过以下问题: JavaScript或jQuery是否可以对多个条件排序JSON数组?
I think I may have made a mistake in asking the question because I specified a JavaScript array, rather than an object, and it appears that this difference is the source of the difficulty I'm experiencing. 我想我可能在问这个问题上犯了一个错误,因为我指定了JavaScript数组而不是对象,而且这种差异似乎是我遇到困难的根源。
I have the following object defined by JSON that exists on my page, which is used for a AutoComplete functionality: 我的页面上存在由JSON定义的以下对象,该对象用于自动完成功能:
var fundList = [
{ "name": "Pension Managed Fund 1", "id": 21, "cid": "N0", "dupId": 53 },
{ "name": "Managed Property Fund 2", "id": 407, "cid": "N0", "dupId": 58 },
{ "name": "Retirement Managed Fund 3", "id": 640, "cid": "N0", "dupId": 111 },
{ "name": "Retirement Managed Fund 4", "id": 752, "cid": "N0", "dupId": 115 }
]
I need to sort these items alphabetically based on their names, but they also have to be ordered by the relevance to the value entered into a textbox (which triggers the automcomplete) 我需要根据它们的名称按字母顺序对这些项目进行排序,但是它们也必须根据与输入文本框的值的相关性进行排序(这会触发自动完成)
So for example, if the user types "Managed" into the textbox, the list above would be sorted as follows: 因此,例如,如果用户在文本框中键入“ Managed”,则上面的列表将按以下方式排序:
Managed Property Fund 2
Pension Managed Fund 1
Retirement Managed Fund 3
Retirement Managed Fund 4
If they typed "Retirement", the list would be sorted: 如果他们键入“退休”,则该列表将进行排序:
Retirement Managed Fund 3
Retirement Managed Fund 4
Managed Property Fund 2
Pension Managed Fund 1
If "Fund" was typed, the order would be normal alphabetical order: 如果键入“ Fund”,则顺序将是正常的字母顺序:
Managed Property Fund 2
Pension Managed Fund 1
Retirement Managed Fund 3
Retirement Managed Fund 4
What do I need to do to be able to sort this based on the name and the criteria I specified? 为了能够根据我指定的名称和条件对此进行排序,我需要做些什么? I've tried to use fundList.sort(new function(a, b)
as suggested in the previous question, but that returns a function undefined error. 我已经尝试使用上一个问题中的建议来使用fundList.sort(new function(a, b)
,但这会返回函数未定义的错误。
You should seriously read up on how to sort arrays in javascript. 您应该认真阅读如何在javascript中对数组进行排序 。
Here's one way to do it: 这是一种实现方法:
var fundList = [
{ "name": "Pension Managed Fund 1", "id": 21, "cid": "N0", "dupId": 53 },
{ "name": "Managed Property Fund 2", "id": 407, "cid": "N0", "dupId": 58 },
{ "name": "Retirement Managed Fund 3", "id": 640, "cid": "N0", "dupId": 111 },
{ "name": "Retirement Managed Fund 4", "id": 752, "cid": "N0", "dupId": 115 }
],
textEntered = 'Managed';
fundList.sort(function(a, b)
{
var aStart = a.name.match(new RegExp('^'+textEntered, 'i')) || [],
bStart = b.name.match(new RegExp('^'+textEntered, 'i')) || [];
if ( aStart.length != bStart.length ) return bStart.length - aStart.length;
else return a.name > b.name ? 1 : -1;
});
var fundList = [
{ "name": "Pension Managed Fund 1", "id": 21, "cid": "N0", "dupId": 53 },
{ "name": "Managed Property Fund 2", "id": 407, "cid": "N0", "dupId": 58 },
{ "name": "Retirement Managed Fund 3", "id": 640, "cid": "N0", "dupId": 111 },
{ "name": "Retirement Managed Fund 4", "id": 752, "cid": "N0", "dupId": 115 }
],
textEntered = 'Managed';
fundList.sort(function(a, b)
{
ai = a.name.indexOf(textEntered);
bi = b.name.indexOf(textEntered);
if(ai>=0 && bi<0) return -1;
else if(bi>=0 && ai<0) return 1;
else return a.name - b.name;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.