开发者

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?

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.

I have the following object defined by JSON that exists on my page, which is used for a AutoComplete functionality:

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 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:

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.


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;
});


You should seriously read up on how to sort arrays in 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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜