简体   繁体   English

如何根据名称对JavaScript对象进行排序,然后按相关性排序?

[英]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;
});

http://jsfiddle.net/txJVB/3/ http://jsfiddle.net/txJVB/3/

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.

相关问题 如何在javascript中的嵌套对象中对键的顺序进行排序? - How can I sort the order of key in a nested object in javascript? 如何根据值对JavaScript中的对象进行排序? - How can I sort an object in JavaScript based on value? 如何使用排序 function 按名称对对象的元素进行排序并提取值? - How can I use the sort function to sort the object's elements in order by name and extract the values? 根据名称和顺序参数在 JavaScript 中对数组进行排序 - Sort array in JavaScript based on name and order parameter JavaScript如何根据用户点击次数排序链接保存到本地? - How can I sort the order of links based on the user's number of clicks and save it to local storage using JavaScript? 如何在 javascript 中按相关性对搜索结果进行排序 - How to sort search results by relevance in javascript 根据搜索词按相关性合并和排序多个JavaScript数组 - Merge and sort multiple JavaScript arrays by relevance based on a search term 如何对对象进行排序,以使所有值都按升序排列? - How Can I Sort an Object so that all the values are in ascending order? 如何确保稳定的array.sort()顺序,javascript - How can I ensure a stable array.sort() order, javascript 如何基于JavaScript中的“之前”和“之后”条件对数组进行排序? - How can I sort an array based on “before” and “after” conditions in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM