好的, 我来为您提供一些 TypeScript 中使用 sort () 方法的高级编程实例:

  1. 对对象数组按多个属性排序:
interface Person {
  name: string;
  age: number;
  height: number;
}
 
const people: Person[] = [
  { name: "Alice", age: 30, height: 165 },
  { name: "Bob", age: 25, height: 180 },
  { name: "Charlie", age: 35, height: 175 },
  { name: "David", age: 30, height: 170 },
];
 
people.sort((a, b) => {
  if (a.age !== b.age) {
    return a.age - b.age; // 首先按年龄排序
  }
  return b.height - a.height; // 如果年龄相同,则按身高降序排序
});
 
console.log(people);
  1. 使用自定义比较函数:
function compareStrings(a: string, b: string): number {
  return a.localeCompare(b, undefined, { sensitivity: 'base' });
}
 
const fruits = ['Apple', 'banana', 'Cherry', 'date'];
fruits.sort(compareStrings);
 
console.log(fruits); // ['Apple', 'banana', 'Cherry', 'date']
  1. 对日期数组排序:
const dates: Date[] = [
  new Date('2023-05-15'),
  new Date('2022-12-01'),
  new Date('2023-01-10'),
  new Date('2022-08-22'),
];
 
dates.sort((a, b) => a.getTime() - b.getTime());
 
console.log(dates);
  1. 使用 Intl. Collator 进行本地化排序:
const items = ['Ärger', 'Zorn', 'Ärztlich', 'Über'];
 
const collator = new Intl.Collator('de');
items.sort(collator.compare);
 
console.log(items); // ['Ärger', 'Ärztlich', 'Über', 'Zorn']
  1. 稳定排序实现:
function stableSort<T>(arr: T[], compare: (a: T, b: T) => number): T[] {
  return arr
    .map((item, index) => ({ item, index }))
    .sort((a, b) => compare(a.item, b.item) || a.index - b.index)
    .map(({ item }) => item);
}
 
const data = [
  { id: 1, value: 'B' },
  { id: 2, value: 'A' },
  { id: 3, value: 'B' },
  { id: 4, value: 'A' },
];
 
const sortedData = stableSort(data, (a, b) => a.value.localeCompare(b.value));
console.log(sortedData);

这些示例展示了 TypeScript 中 sort () 方法的一些高级用法, 包括多属性排序、自定义比较函数、日期排序、本地化排序以及稳定排序的实现。这些技巧可以帮助你在各种复杂场景中高效地进行数据排序。

当然, 我很乐意为您提供一些更复杂的使用 sort 的案例:

  1. 对嵌套对象数组进行多级排序:
interface Department {
  name: string;
  employees: Employee[];
}
 
interface Employee {
  name: string;
  age: number;
  salary: number;
}
 
const departments: Department[] = [
  {
    name: "IT",
    employees: [
      { name: "Alice", age: 30, salary: 75000 },
      { name: "Bob", age: 25, salary: 65000 },
    ],
  },
  {
    name: "HR",
    employees: [
      { name: "Charlie", age: 35, salary: 80000 },
      { name: "David", age: 28, salary: 70000 },
    ],
  },
];
 
departments.sort((a, b) => {
  // 首先按部门名称排序
  const deptComparison = a.name.localeCompare(b.name);
  if (deptComparison !== 0) return deptComparison;
 
  // 如果部门名称相同,则按员工平均工资降序排序
  const avgSalaryA = a.employees.reduce((sum, emp) => sum + emp.salary, 0) / a.employees.length;
  const avgSalaryB = b.employees.reduce((sum, emp) => sum + emp.salary, 0) / b.employees.length;
  return avgSalaryB - avgSalaryA;
});
 
// 对每个部门的员工进行排序
departments.forEach(dept => {
  dept.employees.sort((a, b) => {
    // 首先按年龄升序排序
    const ageComparison = a.age - b.age;
    if (ageComparison !== 0) return ageComparison;
 
    // 如果年龄相同,则按工资降序排序
    return b.salary - a.salary;
  });
});
 
console.log(JSON.stringify(departments, null, 2));
  1. 使用自定义排序规则和权重:
interface Product {
  name: string;
  price: number;
  rating: number;
  sales: number;
}
 
const products: Product[] = [
  { name: "Laptop", price: 1000, rating: 4.5, sales: 1000 },
  { name: "Phone", price: 500, rating: 4.8, sales: 5000 },
  { name: "Tablet", price: 300, rating: 4.2, sales: 2000 },
  { name: "Smartwatch", price: 200, rating: 4.0, sales: 3000 },
];
 
function calculateScore(product: Product): number {
  // 自定义评分算法
  const priceScore = 1 / product.price; // 价格越低,分数越高
  const ratingScore = product.rating / 5; // 评分标准化到0-1
  const salesScore = Math.log10(product.sales) / 4; // 销量取对数并标准化
 
  // 设置权重
  const weights = { price: 0.3, rating: 0.4, sales: 0.3 };
 
  // 计算总分
  return (
    priceScore * weights.price +
    ratingScore * weights.rating +
    salesScore * weights.sales
  );
}
 
products.sort((a, b) => calculateScore(b) - calculateScore(a));
 
console.log(products);
  1. 实现一个通用的多字段排序函数:
type SortDirection = 'asc' | 'desc';
 
interface SortConfig<T> {
  field: keyof T;
  direction: SortDirection;
}
 
function multiFieldSort<T>(arr: T[], sortConfigs: SortConfig<T>[]): T[] {
  return [...arr].sort((a, b) => {
    for (const config of sortConfigs) {
      const { field, direction } = config;
      const valueA = a[field];
      const valueB = b[field];
 
      if (valueA === valueB) continue;
 
      const comparison = direction === 'asc'
        ? compareValues(valueA, valueB)
        : compareValues(valueB, valueA);
 
      if (comparison !== 0) return comparison;
    }
    return 0;
  });
}
 
function compareValues(a: any, b: any): number {
  if (typeof a === 'string' && typeof b === 'string') {
    return a.localeCompare(b);
  }
  return a < b ? -1 : a > b ? 1 : 0;
}
 
// 使用示例
interface User {
  name: string;
  age: number;
  registrationDate: Date;
}
 
const users: User[] = [
  { name: "Alice", age: 30, registrationDate: new Date("2022-01-15") },
  { name: "Bob", age: 25, registrationDate: new Date("2022-03-10") },
  { name: "Charlie", age: 35, registrationDate: new Date("2021-12-05") },
  { name: "David", age: 30, registrationDate: new Date("2022-02-20") },
];
 
const sortedUsers = multiFieldSort(users, [
  { field: 'age', direction: 'desc' },
  { field: 'registrationDate', direction: 'asc' },
  { field: 'name', direction: 'asc' }
]);
 
console.log(sortedUsers);
  1. 实现一个支持分页的排序函数:
interface SortableItem {
  id: number;
  [key: string]: any;
}
 
interface PaginationOptions {
  page: number;
  pageSize: number;
}
 
function sortAndPaginate<T extends SortableItem>(
  items: T[],
  sortField: keyof T,
  sortDirection: 'asc' | 'desc',
  { page, pageSize }: PaginationOptions
): { data: T[], totalPages: number, currentPage: number } {
  const sortedItems = [...items].sort((a, b) => {
    const valueA = a[sortField];
    const valueB = b[sortField];
    
    if (typeof valueA === 'string' && typeof valueB === 'string') {
      return sortDirection === 'asc'
        ? valueA.localeCompare(valueB)
        : valueB.LocaleCompare (valueA);
    }
    
    Return sortDirection === 'asc'
      ? (valueA < valueB ? -1 : valueA > valueB ? 1 : 0)
      : (valueB < valueA ? -1 : valueB > valueA ? 1 : 0);
  });
 
  Const totalPages = Math.Ceil (sortedItems. Length / pageSize);
  Const startIndex = (page - 1) * pageSize;
  Const endIndex = startIndex + pageSize;
  Const paginatedItems = sortedItems.Slice (startIndex, endIndex);
 
  Return {
    Data: paginatedItems,
    TotalPages,
    CurrentPage: page
  };
}
 
// 使用示例
Const data = [
  { id: 1, name: 'Alice', score: 85 },
  { id: 2, name: 'Bob', score: 92 },
  { id: 3, name: 'Charlie', score: 78 },
  { id: 4, name: 'David', score: 95 },
  { id: 5, name: 'Eve', score: 88 },
];
 
Const result = sortAndPaginate (data, 'score', 'desc', { page: 1, pageSize: 2 });
Console.Log (result);

这些复杂案例展示了如何在 TypeScript 中使用 sort () 方法处理更高级的排序需求, 包括多级排序、自定义排序算法、通用多字段排序以及结合分页的排序。这些技巧可以帮助你在实际项目中处理各种复杂的数据排序场景。


有关 TypeScript 之 Sort 方法的更复杂的案例

这个例子是用Claude 3.5生成的

当然可以。让我们通过一个更复杂的案例来深入探讨 TypeScript 的高级特性,同时展示更复杂的排序逻辑。我们将创建一个灵活的、可配置的排序系统,它能够处理多种数据类型和排序规则。

首先,让我们定义一些更复杂的数据结构和类型:

// 定义一个通用的排序方向类型
type SortDirection = 'asc' | 'desc';
 
// 定义一个学生类型,包含更多属性
interface Student {
    id: number;
    name: string;
    age: number;
    grade: number;
    subjects: string[];
    averageScore: number;
    lastTestDate: Date;
}
 
// 定义一个排序配置接口
interface SortConfig<T> {
    key: keyof T;
    direction: SortDirection;
}
 
// 定义一个通用的排序函数类型
type SortFunction<T> = (a: T, b: T) => number;
 
// 定义一个排序器类
class Sorter<T> {
    private sortFunctions: Map<keyof T, SortFunction<T>> = new Map();
 
    constructor(private data: T[]) {}
 
    // 添加自定义排序函数
    addSortFunction<K extends keyof T>(key: K, fn: SortFunction<T>): this {
        this.sortFunctions.set(key, fn);
        return this;
    }
 
    // 根据配置进行排序
    sort(configs: SortConfig<T>[]): T[] {
        return [...this.data].sort((a, b) => {
            for (const config of configs) {
                const { key, direction } = config;
                let result: number;
 
                if (this.sortFunctions.has(key)) {
                    result = this.sortFunctions.get(key)!(a, b);
                } else {
                    result = this.defaultSort(a[key], b[key]);
                }
 
                if (result !== 0) {
                    return direction === 'asc' ? result : -result;
                }
            }
            return 0;
        });
    }
 
    private defaultSort(a: any, b: any): number {
        if (a instanceof Date && b instanceof Date) {
            return a.getTime() - b.getTime();
        }
        if (typeof a === 'string' && typeof b === 'string') {
            return a.localeCompare(b);
        }
        return (a < b ? -1 : (a > b ? 1 : 0));
    }
}
 
// 创建示例数据
const students: Student[] = [
    { id: 1, name: "Alice", age: 20, grade: 3, subjects: ["Math", "Physics"], averageScore: 85, lastTestDate: new Date("2023-05-15") },
    { id: 2, name: "Bob", age: 22, grade: 4, subjects: ["Chemistry", "Biology"], averageScore: 78, lastTestDate: new Date("2023-05-10") },
    { id: 3, name: "Charlie", age: 19, grade: 2, subjects: ["English", "History"], averageScore: 92, lastTestDate: new Date("2023-05-12") },
    { id: 4, name: "David", age: 21, grade: 3, subjects: ["Physics", "Math"], averageScore: 88, lastTestDate: new Date("2023-05-14") },
    { id: 5, name: "Eve", age: 20, grade: 3, subjects: ["Biology", "Chemistry"], averageScore: 85, lastTestDate: new Date("2023-05-13") }
];
 
// 创建排序器实例
const sorter = new Sorter(students);
 
// 添加自定义排序函数
sorter.addSortFunction('subjects', (a, b) => a.subjects.length - b.subjects.length);
 
// 定义排序配置
const sortConfigs: SortConfig<Student>[] = [
    { key: 'grade', direction: 'desc' },
    { key: 'averageScore', direction: 'desc' },
    { key: 'age', direction: 'asc' },
    { key: 'subjects', direction: 'desc' },
    { key: 'lastTestDate', direction: 'desc' },
    { key: 'name', direction: 'asc' }
];
 
// 执行排序
const sortedStudents = sorter.sort(sortConfigs);
 
console.log(sortedStudents);

这个例子展示了以下 TypeScript 的高级特性:

  1. 泛型Sorter<T> 类使用泛型,使其可以处理任何类型的数据。

  2. 类型安全:使用 keyof T 确保我们只能使用 T 类型的有效键进行排序。

  3. 映射类型SortConfig<T> 接口使用 keyof T 来确保配置中的键是有效的。

  4. 函数类型:定义了 SortFunction<T> 类型来表示排序函数。

  5. 类型断言:在 sort 方法中使用 ! 断言操作符来告诉 TypeScript 编译器某个值不会是 undefined。

  6. 方法链addSortFunction 方法返回 this,允许方法链式调用。

  7. 元组类型:虽然这个例子中没有直接使用,但你可以轻松扩展以支持元组类型的排序。

  8. 字面量类型SortDirection 类型使用字面量类型’asc’和’desc’。

这个排序系统非常灵活,可以处理各种复杂的排序需求。它允许你定义多个排序条件,每个条件都可以有自己的排序方向。此外,你还可以为特定的键添加自定义的排序函数。

这个例子不仅展示了复杂的排序逻辑,还充分利用了 TypeScript 的类型系统来确保类型安全和代码的可维护性。通过使用泛型和高级类型,我们创建了一个可以适用于各种数据类型的通用排序系统。

下面这个较复杂的例子是用 Gemini 1.5 Pro (0801) 生成的,相对上面的例子来说,可以明显看出来简单多了。

当然可以!让我们再深入一些,添加更多的复杂性和使用 TypeScript 的特性来提高代码的可读性和可维护性。

假设我们现在要对学生数组进行更复杂的排序规则,比如:

  1. 首先按成绩从高到低排序;
  2. 如果成绩相同,则按年龄升序排序;
  3. 如果成绩和年龄都相同,则按姓名的字典顺序排序;
  4. 但是,某些学生有特殊优先级,他们应该优先于其他学生(不论成绩和年龄)。

为了实现这个需求,我们可以引入更多的类型安全特性和辅助函数。

首先,定义学生类型,并包括一个可选的 priority 字段来表示特殊优先级:

type Student = {
    name: string;
    age: number;
    score: number;
    priority?: boolean; // 可选字段,表示是否有特殊优先级
};
 
const students: Student[] = [
    { name: "Alice", age: 20, score: 88 },
    { name: "Bob", age: 22, score: 90 },
    { name: "Charlie", age: 20, score: 88 },
    { name: "David", age: 19, score: 88, priority: true },
    { name: "Eve", age: 21, score: 85, priority: true },
];

接着,我们创建一个辅助函数来进行比较:

function compareStudents(a: Student, b: Student): number {
    // 优先处理特殊优先级的学生
    if (a.priority && !b.priority) return -1;
    if (!a.priority && b.priority) return 1;
    
    // 按成绩降序
    if (a.score !== b.score) {
        return b.score - a.score;
    }
 
    // 成绩相同,按年龄升序
    if (a.age !== b.age) {
        return a.age - b.age;
    }
    
    // 成绩和年龄都相同,按姓名字典顺序
    return a.name.localeCompare(b.name);
}

然后使用这个辅助函数进行排序:

students.sort(compareStudents);

排序后的 students 数组将是:

[
    { name: "David", age: 19, score: 88, priority: true },
    { name: "Eve", age: 21, score: 85, priority: true },
    { name: "Bob", age: 22, score: 90 },
    { name: "Alice", age: 20, score: 88 },
    { name: "Charlie", age: 20, score: 88 }
]

这个案例展示了如何在 TypeScript 中结合类型定义、可选字段以及辅助函数来实现更复杂的排序逻辑。此外,使用类型定义和辅助函数也使代码更加清晰可维护。


当然,让我为您提供一些TypeScript中 sort 方法的高级应用实例。这些例子将展示如何使用 sort 方法来处理复杂的数据结构和排序逻辑。

示例1: 多条件排序

假设我们有一个包含学生信息的数组,我们想要按照多个条件进行排序:首先按照年级降序,然后按照GPA降序,最后按照姓名字母顺序升序。

interface Student {
  name: string;
  grade: number;
  gpa: number;
}
 
const students: Student[] = [
  { name: "Alice", grade: 12, gpa: 3.5 },
  { name: "Bob", grade: 11, gpa: 3.8 },
  { name: "Charlie", grade: 12, gpa: 3.5 },
  { name: "David", grade: 10, gpa: 4.0 },
  { name: "Eve", grade: 11, gpa: 3.9 }
];
 
students.sort((a, b) => {
  if (a.grade !== b.grade) {
    return b.grade - a.grade; // 年级降序
  }
  if (a.gpa !== b.gpa) {
    return b.gpa - a.gpa; // GPA降序
  }
  return a.name.localeCompare(b.name); // 姓名升序
});
 
console.log(students);

示例2: 自定义排序函数

假设我们有一个包含产品信息的数组,我们想要根据产品的类别和价格进行排序,其中某些类别有特定的排序优先级。

interface Product {
  name: string;
  category: string;
  price: number;
}
 
const products: Product[] = [
  { name: "Laptop", category: "Electronics", price: 1000 },
  { name: "Book", category: "Books", price: 20 },
  { name: "Smartphone", category: "Electronics", price: 500 },
  { name: "Shirt", category: "Clothing", price: 30 },
  { name: "Headphones", category: "Electronics", price: 100 }
];
 
const categoryPriority: { [key: string]: number } = {
  "Electronics": 1,
  "Books": 2,
  "Clothing": 3
};
 
products.sort((a, b) => {
  // 首先按类别优先级排序
  const categoryDiff = (categoryPriority[a.category] || Infinity) - (categoryPriority[b.category] || Infinity);
  if (categoryDiff !== 0) return categoryDiff;
 
  // 同类别内按价格降序排序
  return b.price - a.price;
});
 
console.log(products);

示例3: 排序对象数组中的嵌套数组

假设我们有一个包含用户信息的数组,每个用户有一个订单数组。我们想要按照用户的总订单金额降序排序。

interface Order {
  id: number;
  amount: number;
}
 
interface User {
  id: number;
  name: string;
  orders: Order[];
}
 
const users: User[] = [
  { id: 1, name: "Alice", orders: [{ id: 1, amount: 100 }, { id: 2, amount: 200 }] },
  { id: 2, name: "Bob", orders: [{ id: 3, amount: 50 }, { id: 4, amount: 150 }] },
  { id: 3, name: "Charlie", orders: [{ id: 5, amount: 300 }] }
];
 
users.sort((a, b) => {
  const totalA = a.orders.reduce((sum, order) => sum + order.amount, 0);
  const totalB = b.orders.reduce((sum, order) => sum + order.amount, 0);
  return totalB - totalA; // 总订单金额降序
});
 
console.log(users);

示例4: 使用Intl.Collator进行本地化排序

当需要对字符串进行排序时,尤其是涉及不同语言时,使用Intl.Collator可以提供更准确的本地化排序。

interface Person {
  name: string;
  country: string;
}
 
const people: Person[] = [
  { name: "Étienne", country: "France" },
  { name: "Ángel", country: "Spain" },
  { name: "Zoe", country: "USA" },
  { name: "Åsa", country: "Sweden" }
];
 
const collator = new Intl.Collator(undefined, { sensitivity: 'base' });
 
people.sort((a, b) => collator.compare(a.name, b.name));
 
console.log(people);

这些示例展示了TypeScript中sort方法的一些高级应用,包括多条件排序、自定义排序逻辑、处理嵌套数据结构以及使用Intl.Collator进行本地化排序。这些技术可以帮助你处理更复杂的排序需求。