English 中文(简体)
各种物体,按属性接口的排序优先顺序排列
原标题:Sort array of object by cascading priority of attributes interfaces

I want to sort an array of object (of the same interface) firstly sorting by one attribute of this interface, if these attributes are equals then sorting by secondly attribute, then sorting by a third attribute and so on. And all in typescript mode.

我尝试的解决办法:

  1. 定义:

    Sorter = (a: T, b: T) => number;

例:

const stateSorter = (aState: number, bState: number) => (bState < aState? -1 : 1);
const dateSorter = (aDate: Date, bDate: Date) => (aDate >= bDate ? -1 : 1);
  1. 1. 围绕T接口的属性界定一个分类器

    interface SortSupplier<T, R> { supplier: (container: T) => R; sorter: Sorter< R >; }

例:

interface StateAndDate {
  stateId: ClientStateIdEnum;
  registrationDate: Date;
}
const sorterBySupplier: SortSupplier<StateAndDate, Date> = {supplier: container => container.registrationDate, sorter: dateSorter}

第一项错误企图:

采用多种优先等级:

const sortBySorters1 = <T>(a: T, b: T, orderedSorters: SortSupplier<T, any>[]): number => {
  let sortResult = 0;
  orderedSorters.some(supplierSorter => {
    类别Result = 供应商Sorter.sorter(supplierSorter.supplier(a),供应商Sorter.supplier(b);
    return sortResult !== 0;
  });
  return sortResult;
}

一个错误的例子就是:

(a: StateAndDate, b: StateAndDate) => {
  return sortBySorters1(a, b, [
    {supplier: container => container.stateId, sorter: stateSorter},
    {supplier: container => container.registrationDate, sorter: stateSorter}
  ]);
}

之所以错了,是因为第二类人必须按日期分类,但因使用任何作为第二大类的SortSupplier而按数量(国家)接受其他不同类型。

第二次错误企图:

创建各类可能的特性,例如,数字和日期:

type SortSupplierType<T> = SortSupplier<T, Date>|SortSupplier<T, number>;

然后,以前的错误分类组合:

{supplier: container => container.registrationDate, sorter: stateSorter} 

给出汇编错误,唯一的合规解决办法是:

(a: StateAndDate, b: StateAndDate) => {
  return sortBySorters1(a, b, [
    {supplier: container => container.stateId, sorter: stateSorter},
    {supplier: container => container.registrationDate, sorter: dateSorter}
  ]);
}

但:

const sortBySorters2 = <T>(a: T, b: T, orderedSorters: SortSupplierType<T>[]): number => {
  let sortResult = 0;
  orderedSorters.some(supplierSorter => {
    类别Result = 供应商Sorter.sorter(supplierSorter.supplier(a),供应商Sorter.supplier(b);
    return sortResult !== 0;
  });
  return sortResult;
}

但它给出了下一个汇编错误:

序号的标示不能归入“日期”的参数;编号。

类型编号不能划入“日期”和编号。

类型编号不能按日期类型分配。

类别Result = 供应商Sorter.sorter(supplierSorter.supplier(a),供应商Sorter.supplier(b);

¿Someone有一种普遍的解决办法,可以解决围绕 cas式的先天性交错的此类问题?


(为我的英语教授)

问题回答

你们可以接受这些项目和钥匙,认为它们是履行职能的论据,然后就关键事项进行仲裁,并根据每个项目的价值进行排序。 如果对钥匙的数值相同,则转入下一个钥匙。

interface Item {
  a: string;
  b: number;
  c: Date;
}

function sortByKeys<T>(items: T[], ...keys: (keyof T)[]): void {
  items.sort((item1, item2) => {
    for (const key of keys) {
      const v1 = item1[key];
      const v2 = item2[key];
      if (v1 < v2) return -1;
      if (v2 < v1) return 1;
    }
    return 0;
  });
}

const items: Item[] = [
  { a:  aa , b: 1, c: new Date() },
  { a:  a , b: 2, c: new Date() },
  { a:  a , b: 1, c: new Date() },
];

sortByKeys(items,  b ,  a );

console.log(items);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签