• <i id='t36rK'><tr id='t36rK'><dt id='t36rK'><q id='t36rK'><span id='t36rK'><b id='t36rK'><form id='t36rK'><ins id='t36rK'></ins><ul id='t36rK'></ul><sub id='t36rK'></sub></form><legend id='t36rK'></legend><bdo id='t36rK'><pre id='t36rK'><center id='t36rK'></center></pre></bdo></b><th id='t36rK'></th></span></q></dt></tr></i><div id='t36rK'><tfoot id='t36rK'></tfoot><dl id='t36rK'><fieldset id='t36rK'></fieldset></dl></div>

          <bdo id='t36rK'></bdo><ul id='t36rK'></ul>

        <small id='t36rK'></small><noframes id='t36rK'>

        <legend id='t36rK'><style id='t36rK'><dir id='t36rK'><q id='t36rK'></q></dir></style></legend>

      1. <tfoot id='t36rK'></tfoot>

        根据键值对数组进行排序

        时间:2023-07-31

              <legend id='T6EiF'><style id='T6EiF'><dir id='T6EiF'><q id='T6EiF'></q></dir></style></legend>

                <small id='T6EiF'></small><noframes id='T6EiF'>

                • <bdo id='T6EiF'></bdo><ul id='T6EiF'></ul>
                  <tfoot id='T6EiF'></tfoot>
                • <i id='T6EiF'><tr id='T6EiF'><dt id='T6EiF'><q id='T6EiF'><span id='T6EiF'><b id='T6EiF'><form id='T6EiF'><ins id='T6EiF'></ins><ul id='T6EiF'></ul><sub id='T6EiF'></sub></form><legend id='T6EiF'></legend><bdo id='T6EiF'><pre id='T6EiF'><center id='T6EiF'></center></pre></bdo></b><th id='T6EiF'></th></span></q></dt></tr></i><div id='T6EiF'><tfoot id='T6EiF'></tfoot><dl id='T6EiF'><fieldset id='T6EiF'></fieldset></dl></div>

                    <tbody id='T6EiF'></tbody>
                  本文介绍了根据键值对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个当前按名称排序的函数和一个值/键对数组.

                  I have a function which sorts by name currently and an array of value / key pairs.

                  我想知道如何传递正在执行的排序的键,以便每次都可以调用相同的函数:

                  I wonder how can I pass the key on which sort is being performed so I can call the same function every time like so:

                  var arr = [{name:'bob', artist:'rudy'},
                             {name:'johhny', artist:'drusko'},
                             {name:'tiff', artist:'needell'},
                             {name:'top', artist:'gear'}];
                  
                  sort(arr, 'name');   //trying to sort by name
                  sort(arr, 'artist'); //trying to sort by artist
                  
                  function sort(arr) {
                    arr.sort(function(a, b) {
                      var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase();
                      if (nameA < nameB) //sort string ascending
                        return -1;
                      if (nameA > nameB)
                        return 1;
                      return 0; //default return value (no sorting)
                     });          
                  }
                  

                  推荐答案

                  [edit 2020/08/14] 这是一个相当老的答案,也不是很好,所以进行了简化和修改.

                  [edit 2020/08/14] This was rather an old answer and not very good as well, so simplified and revised.

                  创建一个返回排序 lambda 的函数(执行实际排序的 Array.prototype.sort 回调).该函数可以接收键名、排序类型(字符串(区分大小写或不区分大小写)或数字)和排序顺序(升序/降序).lambda 使用参数值(闭包)来确定如何排序.

                  Create a function that returns the sorting lambda (the Array.prototype.sort callback that does the actual sorting). That function can receive the key name, the kind of sorting (string (case sensitive or not) or numeric) and the sorting order (ascending/descending). The lambda uses the parameter values (closure) to determine how to sort.

                  const log = (...strs) => 
                    document.querySelector("pre").textContent += `
                  ${strs.join("
                  ")}`;
                  const showSortedValues = (arr, key) => 
                    ` => ${arr.reduce((acc, val) => ([...acc, val[key]]), [])}`;
                    
                  // the actual sort lamda factory function
                  const sortOnKey = (key, string, desc) => {
                    const caseInsensitive = string && string === "CI";
                    return (a, b) => {
                      a = caseInsensitive ? a[key].toLowerCase() : a[key];
                      b = caseInsensitive ? b[key].toLowerCase() : b[key];
                      if (string) {
                        return desc ? b.localeCompare(a) : a.localeCompare(b);
                      }
                      return desc ? b - a : a - b;
                    }
                  };
                  
                  // a few examples
                  const onNameStringAscendingCaseSensitive = 
                    getTestArray().sort( sortOnKey("name", true) );
                  const onNameStringAscendingCaseInsensitive = 
                    getTestArray().sort( sortOnKey("name", "CI", true) );
                  const onValueNumericDescending = 
                    getTestArray().sort( sortOnKey("value", false, true) );
                  
                  // examples
                  log(`*key = name, string ascending case sensitive`,
                    showSortedValues(onNameStringAscendingCaseSensitive, "name")
                  );
                  
                  log(`
                  *key = name, string descending case insensitive`,
                    showSortedValues(onNameStringAscendingCaseInsensitive, "name")
                  );
                  
                  log(`
                  *key = value, numeric desc`, 
                    showSortedValues(onValueNumericDescending, "value")
                  );
                  
                  function getTestArray() {
                    return [{
                      name: 'Bob',
                      artist: 'Rudy',
                      value: 23,
                    }, {
                      name: 'John',
                      artist: 'Drusko',
                      value: 123,
                    }, {
                      name: 'Tiff',
                      artist: 'Needell',
                      value: 1123,
                    }, {
                      name: 'Top',
                      artist: 'Gear',
                      value: 11123,
                    }, {
                      name: 'john',
                      artist: 'Johanson',
                      value: 12,
                    }, ];
                  }

                  <pre></pre>

                  这篇关于根据键值对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何避免带有 p 标签的新行? 下一篇:“href"是什么意思?在 HTML 中代表什么?

                  相关文章

                  1. <legend id='K4sf9'><style id='K4sf9'><dir id='K4sf9'><q id='K4sf9'></q></dir></style></legend>

                  2. <tfoot id='K4sf9'></tfoot>
                    <i id='K4sf9'><tr id='K4sf9'><dt id='K4sf9'><q id='K4sf9'><span id='K4sf9'><b id='K4sf9'><form id='K4sf9'><ins id='K4sf9'></ins><ul id='K4sf9'></ul><sub id='K4sf9'></sub></form><legend id='K4sf9'></legend><bdo id='K4sf9'><pre id='K4sf9'><center id='K4sf9'></center></pre></bdo></b><th id='K4sf9'></th></span></q></dt></tr></i><div id='K4sf9'><tfoot id='K4sf9'></tfoot><dl id='K4sf9'><fieldset id='K4sf9'></fieldset></dl></div>

                    <small id='K4sf9'></small><noframes id='K4sf9'>

                      <bdo id='K4sf9'></bdo><ul id='K4sf9'></ul>