Typescript按状态(字符串值)对列表进行排序

huangapple go评论71阅读模式
英文:

Typescript sorting list by status (string value)

问题

我需要按照状态对项目列表进行排序,但我不确定应该使用哪种方法,是创建比较器还是某种排序技巧?
是否可以在这里使用 lodash?

示例输入列表:

[
{id: 1, name: "name1", status: "Failed"},
{id: 2, name: "name2", status: "Success"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"}
]

示例排序后的列表(输出)按照 Failed -> In progress -> Reserved -> Pending -> Success 排序:

[
{id: 1, name: "name1", status: "Failed"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"},
{id: 2, name: "name2", status: "Success"}
]

我使用的堆栈是 Angular + Typescript。

编辑:列表通常很大,大约有~15k行。

英文:

I need to sort list of items by its status and I am not sure which approach I should go, is it creating comparer or some kind of sorting technique ?
Would lodash work here ?

example input list:

[
{id: 1, name: "name1", status: "Failed"},
{id: 2, name: "name2", status: "Success"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"}
]

example sorted list (output) sorted in Failed -> In progress -> Reserved -> Pending -> Success

[
{id: 1, name: "name1", status: "Failed"},
{id: 3, name: "name3", status: "In progress"},
{id: 4, name: "name4", status: "Reserved"},
{id: 5, name: "name5", status: "Pending"}
{id: 2, name: "name2", status: "Success"},
]

stack I am using is Angular + Typescript.

Edit: List is kinda big, about ~15k rows usually.

答案1

得分: 9

你可以根据另一个数组中的值对你的数组进行排序:

let sortOrder = ['Failed', 'In progress', 'Reserved', 'Pending', 'Success'];
let array = [
    { id: 1, name: "name1", status: "Failed" },
    { id: 2, name: "name2", status: "Success" },
    { id: 3, name: "name3", status: "In progress" },
    { id: 4, name: "name4", status: "Reserved" },
    { id: 5, name: "name5", status: "Pending" }
];

array.sort((a, b) => {
    return sortOrder.indexOf(a.status) - sortOrder.indexOf(b.status);
});

console.log(array);
英文:

You can sort your array based on values in other array:

let sortOrder = ['Failed', 'In progress', 'Reserved', 'Pending', 'Success'];
let array = [
    { id: 1, name: "name1", status: "Failed" },
    { id: 2, name: "name2", status: "Success" },
    { id: 3, name: "name3", status: "In progress" },
    { id: 4, name: "name4", status: "Reserved" },
    { id: 5, name: "name5", status: "Pending" }
];

array.sort((a, b) => {
    return sortOrder.indexOf(a.status) - sortOrder.indexOf(b.status);
});

console.log(array);

答案2

得分: 0

你可以使用以下代码中的 sort() 来根据状态对数组进行排序。

const data = [
  { id: 1, name: "name1", status: "Failed" },
  { id: 2, name: "name2", status: "Success" },
  { id: 3, name: "name3", status: "In progress" },
  { id: 4, name: "name4", status: "Reserved" },
  { id: 5, name: "name5", status: "Pending" }
]

function sortBy(data: any): any {
  const order = ['Failed', 'In progress', 'Reserved', 'Pending', 'Success']
  var ordering: any = {};
  for (var i = 0; i < order.length; i++)
    ordering[order[i]] = i;
  var result = data.sort(function(a: any, b: any) {
    return (ordering[a.status] - ordering[b.status]) || a.status.localeCompare(b.status);
  });
  return result;
}

console.log(sortBy(data));

查看示例 https://repl.it/repls/KnowingRareNlp

英文:

You can sort using sort() as below to sort the array using status.

const data = [
  { id: 1, name: &quot;name1&quot;, status: &quot;Failed&quot; },
  { id: 2, name: &quot;name2&quot;, status: &quot;Success&quot; },
  { id: 3, name: &quot;name3&quot;, status: &quot;In progress&quot; },
  { id: 4, name: &quot;name4&quot;, status: &quot;Reserved&quot; },
  { id: 5, name: &quot;name5&quot;, status: &quot;Pending&quot; }
]

function sortBy(data: any): any {
  const order = [&#39;Failed&#39;, &#39;In progress&#39;, &#39;Reserved&#39;, &#39;Pending&#39;, &#39;Success&#39;]
  var ordering: any = {};
  for (var i = 0; i &lt; order.length; i++)
    ordering[order[i]] = i;
  var result = data.sort(function(a: any, b: any) {
    return (ordering[a.status] - ordering[b.status]) || a.status.localeCompare(b.status);
  });
  return result;
}

console.log(sortBy(data));

Checkout the example https://repl.it/repls/KnowingRareNlp

答案3

得分: 0

要按自定义顺序对数据进行排序,您可以首先对项目进行分组,然后根据您的排序顺序将它们添加到最终输出中。

尝试这样做:

constructor() {
  var groupedData = this.groupBy(this.data, "status");
  var sortArrangement = ["Failed", "In progress", "Reserved", "Pending", "Success"]
  sortArrangement.forEach(item => {
    groupedData[item].forEach(x => {
      this.result.push(x)
    })
  })
  console.log(this.result)
}

groupBy(xs, key) {
  return xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});
}

工作示例

注意:您也可以将此逻辑移到一个管道中。

英文:

To sort the data in custom order, you can group the items first and then based on your sort order, add them to the final output.

Try like this:

  constructor() {
    var groupedData = this.groupBy(this.data, &quot;status&quot;);
    var sortArrangement = [&quot;Failed&quot;, &quot;In progress&quot;, &quot;Reserved&quot;, &quot;Pending&quot;, &quot;Success&quot;]
    sortArrangement.forEach(item =&gt; {
      groupedData[item].forEach(x =&gt; {
        this.result.push(x)
      })
    })
    console.log(this.result)
  }

  groupBy(xs, key) {
    return xs.reduce(function(rv, x) {
      (rv[x[key]] = rv[x[key]] || []).push(x);
      return rv;
    }, {});
  }

Working Demo

Note: You can move this logic to a pipe as well.

答案4

得分: 0

要按任意值排序,创建一个映射或对象,为每个状态分配数值权重,然后使用 Array.sort() 或 lodash 的 _.orderBy(),根据状态从映射中获取权重:

const arr = [
  {"id": 1, "name": "name1", "status": "Failed"},
  {"id": 2, "name": "name2", "status": "Success"},
  {"id": 3, "name": "name3", "status": "In progress"},
  {"id": 4, "name": "name4", "status": "Reserved"},
  {"id": 5, "name": "name5", "status": "Pending"}
];

const order = new Map([
  ['Failed', 0],
  ['In progress', 1],
  ['Reserved', 2],
  ['Pending', 3],
  ['Success', 4]
]);

const result = _.orderBy(arr, o => order.get(o.status));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
英文:

To sort by an arbitrary value, create a Map or an object that gives numeric weight to each status, and then use Array.sort() or lodash's _.orderBy(), and get the weight from the map according to the status:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [{&quot;id&quot;:1,&quot;name&quot;:&quot;name1&quot;,&quot;status&quot;:&quot;Failed&quot;},{&quot;id&quot;:2,&quot;name&quot;:&quot;name2&quot;,&quot;status&quot;:&quot;Success&quot;},{&quot;id&quot;:3,&quot;name&quot;:&quot;name3&quot;,&quot;status&quot;:&quot;In progress&quot;},{&quot;id&quot;:4,&quot;name&quot;:&quot;name4&quot;,&quot;status&quot;:&quot;Reserved&quot;},{&quot;id&quot;:5,&quot;name&quot;:&quot;name5&quot;,&quot;status&quot;:&quot;Pending&quot;}]

const order = new Map([[&#39;Failed&#39;, 0], [&#39;In progress&#39;, 1], [&#39;Reserved&#39;, 2], [&#39;Pending&#39;, 3], [&#39;Success&#39;, 4]])

const result = _.orderBy(arr, o =&gt; order.get(o.status))

console.log(result)

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 14:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定