处理某些案例的Array.prototype.sort()和其他案件

时间:2016-12-30 10:21:05

标签: javascript arrays

我正在尝试排列一系列衣服尺寸。

我使用Array.prototype.sort()函数定义了一个大小的数组,然后按照我在数组中定义的顺序对输入数组进行排序 这适用于标准尺寸,如 S,M和L ,但不适用于 40,42,44 等尺寸。

对数组进行排序

if (inputArray['code'] == 'size'){
            inputArray.sizes.sort(function(a,b) {
                var sizes = ["29","30","31","32","33","34","35","36","37","38","39","40","41","42",
                    "44","46","48","50","52","54","56","58","60",
                    "NT","N-USE",                  "2-3","3-4","4-5","5-6","6-7","7-8","8-9","9-10","10-11","10-13","11-13",
                    "XXXS","XXS","XX/XS","XS","XS/S","S","S/M","M","M/L","L","L/XL","XL","XL/XX","XXL","XXXL"];
                var optionValueA = a['label'];
                var optionValueB = b['label'];
                html += ' index position ' + optionValueA + '=' + sizes.indexOf(optionValueA) + ' ' + optionValueB + '=' + sizes.indexOf(optionValueB);
                return sizes.indexOf(optionValueA)+1 - sizes.indexOf(optionValueB)+1
            });
        }
html += ' SIZES json= ' + JSON.stringify(inputArray.sizes);

这里是标准尺寸的输出(排序良好)

  

指数位置S = 41 M = 43指数位置M = 43 XL = 47指数位置   XL = 47 XXL = 49指数位置XXL = 49 L = 45指数位置XL = 47 L = 45   指数位置M = 43 L = 45 SIZES json =   [{ “ID”: “646”, “标签”: “S”, “产品”:[ “28535”]},{ “ID”: “672”, “标签”: “M”, “产品”: [ “28536”]},{ “ID”: “651”, “标签”: “L”, “产品”:[ “28539”]},{ “ID”: “691”, “标签”:“XL ”, “产品”:[ “28537”]},{ “ID”: “640”, “标签”: “XXL”, “产品”:[ “28538”]}]

这里输出数字大小(排序不好)

  

指数位置42 = 13 44 = 14指数位置42 = 13 46 = 15指数位置   46 = 15 48 = 16索引位置42 = 13 48 = 16索引位置46 = 15 50 = 17   指数位置50 = 17 54 = 19指数位置54 = 19 52 = 18指数位置   50 = 17 52 = 18指数位置46 = 15 52 = 18 SIZES json =   [{ “ID”: “687”, “标签”: “44”, “产品”:[ “23300”]},{ “ID”: “650”, “标签”: “42”, “产品”: [ “23299”]},{ “ID”: “649”, “标签”: “48”, “产品”:[ “23302”]},{ “ID”: “643”, “标签”:“46 ”, “产品”:[ “23301”]},{ “ID”: “688”, “标签”: “52”, “产品”:[ “23305”]},{ “ID”: “669”, “标签”: “50”, “产品”:[ “23303”]},{ “ID”: “684”, “标签”: “54”, “产品”:[ “23304”]}]

这里是原始数组(在排序之前)来自debug

数字尺寸

  

ORIGINAL ARRAY =   [{ “ID”: “650”, “标签”: “42”, “产品”:[ “23299”]},{ “ID”: “687”, “标签”: “44”, “产品”: [ “23300”]},{ “ID”: “643”, “标签”: “46”, “产品”:[ “23301”]},{ “ID”: “649”, “标签”:“48 ”, “产品”:[ “23302”]},{ “ID”: “669”, “标签”: “50”, “产品”:[ “23303”]},{ “ID”: “684”, “标签”: “54”, “产品”:[ “23304”]},{ “ID”: “688”, “标签”: “52”, “产品”:[ “23305”]}]

标准尺寸

  

ORIGINAL ARRAY =   [{ “ID”: “646”, “标签”: “S”, “产品”:[ “28535”]},{ “ID”: “672”, “标签”: “M”, “产品”: [ “28536”]},{ “ID”: “691”, “标签”: “XL”, “产品”:[ “28537”]},{ “ID”: “640”, “标签”:“XXL ”, “产品”:[ “28538”]},{ “ID”: “651”, “标签”: “L”, “产品”:[ “28539”]}]

我不是很熟练的js所以mabe有人可以提供帮助 提前谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用对象作为排序顺序,并在必要时使用默认值。

订单是使用商品和递增的索引构建的。



function orderSize(a, b) {
    return (order[a.label] || 0) - (order[b.label] || 0);
}

var array1 = [{ "id": "640", "label": "XXL", "products": ["28538"] }, { "id": "646", "label": "S", "products": ["28535"] }, { "id": "672", "label": "M", "products": ["28536"] }, { "id": "651", "label": "L", "products": ["28539"] }, { "id": "691", "label": "XL", "products": ["28537"] }],
    array2 = [{ "id": "643", "label": "46", "products": ["23301"] }, { "id": "650", "label": "42", "products": ["23299"] }, { "id": "649", "label": "48", "products": ["23302"] }, { "id": "684", "label": "54", "products": ["23304"] }, { "id": "688", "label": "52", "products": ["23305"] }, { "id": "669", "label": "50", "products": ["23303"] }, { "id": "687", "label": "44", "products": ["23300"] }],
    sizes = ["29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "44", "46", "48", "50", "52", "54", "56", "58", "60", "NT", "N-USE", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10", "10-11", "10-13", "11-13", "XXXS", "XXS", "XX/XS", "XS", "XS/S", "S", "S/M", "M", "M/L", "L", "L/XL", "XL", "XL/XX", "XXL", "XXXL"],
    order = {};

sizes.forEach(function (a, i) { order[a] = i + 1; });

array1.sort(orderSize);
array2.sort(orderSize);

console.log(array1);
console.log(array2);

.as-console-wrapper { max-height: 100% !important; top: 0; }




如果您想对混合尺寸进行排序,您可以直接使用一个对象,其中包含各种尺寸系统的映射排序值,例如

{
    64: 1,
    xs: 2,
    68: 3,
    72: 4
    s:  5
    76: 6
    /* ... */
}

答案 1 :(得分:0)

问题是你的比较函数是inconsistent,特别是部分

return sizes.indexOf(optionValueA)+1 - sizes.indexOf(optionValueB)+1
//                                ^^                              ^^

如果两个比较值相差2个或更少且a应小于b,则函数返回错误的结果(>0而不是<=0) 。这将导致意外的排序。

我认为你的意思是

return (sizes.indexOf(optionValueA)+1) - (sizes.indexOf(optionValueB)+1)

但这是不必要的,可以简化为

return sizes.indexOf(optionValueA) - sizes.indexOf(optionValueB)