迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中如何对集合进行排序

作者:迹忆客 最近更新:2023/02/01 浏览次数:

在 JavaScript 中对集合进行排序:

  1. 使用 Array.from() 方法将 Set 转换为数组。
  2. 使用 Array.sort() 方法对数组进行排序。
  3. 将数组转换回 Set 集合对象。
// ✅ Sort a Set containing Strings

const set1 = new Set(['c', 'b', 'a']);

const sortedArray = Array.from(set1).sort();
console.log(sortedArray); // 👉️ ['a', 'b', 'c']

const sortedSet = new Set(sortedArray);
console.log(sortedSet); // 👉️ {'a', 'b', 'c'}

如果需要对包含数字的 Set 进行排序,则必须将函数传递给 Array.sort() 方法。

// ✅ Sort a Set containing Numbers

const set1 = new Set([300, 100, 700]);

// 👇️ sorts numbers in Ascending order
const sortedArray = Array.from(set1).sort((a, b) => a - b);
console.log(sortedArray); // 👉️ [100, 300, 700]

const sortedSet = new Set(sortedArray);
console.log(sortedSet); // 👉️ {100, 300, 700}

我们使用 Array.from() 方法将 Set 对象转换为数组,因此我们可以对数组调用 Array.sort() 方法。

请注意 ,在对数字进行排序时,我们必须将一个函数作为参数传递给 sort() 方法,而对于字符串,我们不需要。

我们传递给 sort() 方法的参数是一个定义排序顺序的函数。

如果我们不提供该参数,数组元素将转换为字符串并根据其 UTF-816 代码单元值进行排序。

这不是我们在处理包含数字的 Set 对象时想要的,但是,这正是我们在比较字符串时想要的。

对数组进行排序后,我们将其传递给 Set() 构造函数以将其转换回 Set。

如果必须经常这样做,请定义可重用的函数。

function sortStringSet(set) {
  const sortedArray = Array.from(set).sort();

  const sortedSet = new Set(sortedArray);

  return sortedSet;
}

// 👇️ Set(3) { 'a', 'b', 'c' }
console.log(sortStringSet(new Set(['c', 'b', 'a'])));

// 👇️ Set(3) { 'a', 'c', 'z' }
console.log(sortStringSet(new Set(['c', 'z', 'a'])));

sortStringSet() 函数将包含字符串的 Set 作为参数,并对 Set 进行排序。

我们还可以定义 sortNumbersSet() 函数。

function sortNumbersSet(set) {
  const sortedArray = Array.from(set).sort((a, b) => a - b);

  const sortedSet = new Set(sortedArray);

  return sortedSet;
}

// 👇️ Set(3) { 100, 300, 700 }
console.log(sortNumbersSet(new Set([700, 100, 300])));

// 👇️ Set(3) { 100, 500, 600 }
console.log(sortNumbersSet(new Set([500, 600, 100])));

该函数将数字 Set 作为参数并对 Set 进行排序。


使用扩展语法 (...) 对集合进行排序

或者,我们可以使用扩展语法 ... 将 Set 转换为数组。

// ✅ Sort a Set object that contains strings
const set1 = new Set(['c', 'b', 'a']);

const sortedArray = [...set1].sort();
console.log(sortedArray); // 👉️ [ 'a', 'b', 'c' ]

const sortedSet = new Set(sortedArray);
console.log(sortedSet); // 👉️ {'a', 'b', 'c'}

如果我们的 Set 对象包含数字,请将函数传递给 Array.sort() 方法。

const numbersSet = new Set([300, 100, 700]);

const sortedNumbers = [...numbersSet].sort((a, b) => a - b);
console.log(sortedNumbers); // 👉️ [100, 300, 700]

const sortedNumbersSet = new Set(sortedNumbers);
console.log(sortedNumbersSet); // 👉️ {100, 300, 700}

代码示例实现与使用 Array.from() 方法的代码示例相同的结果。

扩展语法 ... 只是将 Set 的元素解包到一个数组中。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便