简体   繁体   English

JavaScript自定义排序功能,用于确定字母的优先级

[英]JavaScript custom sort function to prioritize a letter

I already have a sorted array (can contain up to 1,000 items), I just want to take the block of items that start with the specified character and move them to the top. 我已经有一个排序数组(最多可以包含1,000个项目),我只想获取以指定字符开头的项目块并将它们移到顶部。

// Before sort
{ "alpha", "beta", "delta", "delta frequency", "gamma", "theta" } 

// After sort with "d" as the specified letter
{ "delta", "delta frequency", "alpha", "beta", "gamma", "theta" }

I'm not familiar enough with JavaScript to know a good way to do this. 我不熟悉JavaScript,知道一个很好的方法来做到这一点。 My first thought was to iterate through each item and see if the index of the specified start character was "0" to find the first and last array index, and moving that range to the top of the array, but that seemed like it might be wasteful. 我的第一个想法是迭代每个项目,看看指定的起始字符的索引是否为“0”,以找到第一个和最后一个数组索引,并将该范围移动到数组的顶部,但这似乎可能是浪费。 Is there a better way? 有没有更好的办法?

var array = [ "alpha", "beta", "delta", "delta frequency", "gamma", "theta" ];
var startingWithD = array.filter(function(s) {
    return s[0] == "d";
});
var others = array.filter(function(s) {
    return s[0] != "d";
});
array = startingWithD.concat(others);

Here's an option: 这是一个选项:

function sortWithPriority(arr, letter) {
    var numFound = 0,
        i = arr.length,
        cur;
    while (i-- > numFound) {
        cur = arr[i];
        if (cur.charAt(0).toLowerCase() === letter.toLowerCase()) {
            arr.unshift(arr.splice(i, 1)[0]);
            numFound++;
            i++;
        }
    }
}

DEMO: http://jsfiddle.net/kU8wK/ 演示: http //jsfiddle.net/kU8wK/

It will remove any item that starts with the letter specified, and moves it to the beginning of the array. 它将删除以指定字母开头的任何项目,并将其移动到数组的开头。 So the array is modified in place. 所以数组就地修改了。

It could easily be modified to allow the passing of a string (not just a letter) that the item must start with. 它可以很容易地修改,以允许传递项目必须开始的字符串(而不仅仅是一个字母)。

UPDATE: 更新:

As for performance, when it comes to the current answers of this question, this method seems fastest. 至于性能,当谈到这个问题的当前答案时,这种方法似乎最快。

Here's the performance test I made: http://jsperf.com/sort-with-letter-priority 这是我做的性能测试: http//jsperf.com/sort-with-letter-priority

It uses an array with hundreds of items in alphabetical order (better test case than 10 or so). 它使用一个按字母顺序排列数百个项目的数组(测试用例大于10个左右)。 It may not be the best way to generate the array, but it does it correctly and that part is unrelated to the actual timing anyways (it runs outside of the timing). 它可能不是生成数组的最佳方法,但它可以正确执行,并且该部分与实际时序无关(它在时间之外运行)。

Note that my function is modified to not use toLowerCase , so that all functions should be "equal" when comparing the first letter to the targeted letter. 请注意,我的函数被修改为不使用toLowerCase ,因此在将第一个字母与目标字母进行比较时,所有函数应该“相等”。 Of course, if you want it to be more accurate, in the case that the first letter of the items in the array could be upper case (or you pass an upper case letter), you'd have to use toLowerCase on both to get an accurate comparison. 当然,如果你希望它是更准确,在的情况下,该数组中的项目的第一个字母可能是大写(或你传递一个大写字母),你必须使用toLowerCase得到准确的比较。 For the performance test, I left out that possibility for all test cases. 对于性能测试,我忽略了所有测试用例的可能性。

Here's a sorting algorithm, which will actually sort the array weighting strings that start with "d" to the beginning. 这是一个排序算法,它实际上将以“d”开头的数组加权字符串排序。 Note that I've rearranged the beginning array a bit, to show that the sorting works correctly. 请注意,我已经重新排列了开始数组,以显示排序正常工作。

array = [ "beta", "alpha", "delta frequency", "delta 2", "delta 1", "delta", "gamma", "theta" ];
array.sort(function(a,b) {
    if (a[0] == "d" && b[0] != "d") {
        return -1
    }
    if (b[0] == "d" && a[0] != "d") {
        return 1;
    }
    return a > b;
});

console.log(array);

http://jsfiddle.net/ryanbrill/RqfgL/ http://jsfiddle.net/ryanbrill/RqfgL/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM