您现在的位置是:网站首页 > 数组排序与搜索文章详情
数组排序与搜索
陈川
【
JavaScript
】
15528人已围观
4673字
数组排序
JavaScript提供了多种数组排序方法,最常用的是sort()
方法。默认情况下,sort()
将元素转换为字符串后按Unicode码点排序:
const fruits = ['banana', 'apple', 'orange', 'grape'];
fruits.sort(); // ['apple', 'banana', 'grape', 'orange']
对于数字排序,需要提供比较函数:
const numbers = [40, 100, 1, 5, 25];
numbers.sort((a, b) => a - b); // 升序 [1, 5, 25, 40, 100]
numbers.sort((a, b) => b - a); // 降序 [100, 40, 25, 5, 1]
对象数组排序示例:
const users = [
{ name: 'John', age: 25 },
{ name: 'Alice', age: 20 },
{ name: 'Bob', age: 30 }
];
users.sort((a, b) => a.age - b.age);
// 按年龄升序排列
自定义排序
可以实现更复杂的排序逻辑。例如按字符串长度排序:
const words = ['apple', 'banana', 'kiwi', 'orange'];
words.sort((a, b) => a.length - b.length);
// ['kiwi', 'apple', 'banana', 'orange']
多条件排序:
const products = [
{ name: 'Laptop', price: 999, stock: 5 },
{ name: 'Phone', price: 699, stock: 10 },
{ name: 'Tablet', price: 499, stock: 8 }
];
products.sort((a, b) => {
if (a.price !== b.price) {
return a.price - b.price; // 价格优先
}
return b.stock - a.stock; // 库存次之
});
搜索方法
线性搜索
最基本的搜索方法是遍历数组:
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) return i;
}
return -1;
}
二分搜索
对于已排序数组,二分搜索效率更高:
function binarySearch(sortedArr, target) {
let left = 0;
let right = sortedArr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (sortedArr[mid] === target) return mid;
if (sortedArr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
内置搜索方法
JavaScript提供了多种内置搜索方法:
indexOf()
和lastIndexOf()
:
const numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
numbers.indexOf(3); // 2
numbers.lastIndexOf(3); // 6
find()
和findIndex()
:
const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Alice' },
{ id: 3, name: 'Bob' }
];
const user = users.find(user => user.id === 2); // { id: 2, name: 'Alice' }
const index = users.findIndex(user => user.name === 'Bob'); // 2
includes()
检查是否存在:
const colors = ['red', 'green', 'blue'];
colors.includes('green'); // true
colors.includes('yellow'); // false
性能考虑
不同搜索方法在不同场景下的性能表现:
- 对于小型数组(<100元素),线性搜索足够快
- 大型已排序数组优先考虑二分搜索(O(log n)复杂度)
includes()
比indexOf()
更语义化但性能相当- 频繁搜索可考虑使用Set或Map数据结构
// 使用Set提高搜索性能
const largeArray = [...Array(100000).keys()];
const valueSet = new Set(largeArray);
valueSet.has(99999); // 极快
高级搜索技巧
模糊搜索
实现简单的模糊搜索:
function fuzzySearch(arr, query) {
return arr.filter(item =>
item.toLowerCase().includes(query.toLowerCase())
);
}
const fruits = ['Apple', 'Banana', 'Grape', 'Pineapple'];
fuzzySearch(fruits, 'ap'); // ['Apple', 'Grape', 'Pineapple']
多条件搜索
const products = [
{ name: 'Laptop', category: 'Electronics', price: 999 },
{ name: 'Shirt', category: 'Clothing', price: 25 },
{ name: 'Phone', category: 'Electronics', price: 699 }
];
function searchProducts(products, filters) {
return products.filter(product => {
return Object.entries(filters).every(([key, value]) => {
if (value === undefined) return true;
return product[key] === value;
});
});
}
searchProducts(products, { category: 'Electronics' });
// 返回所有电子产品
排序与搜索结合
实际应用中常需要先排序再搜索:
const books = [
{ title: 'JavaScript Guide', year: 2020 },
{ title: 'CSS Secrets', year: 2015 },
{ title: 'HTML5 Mastery', year: 2018 }
];
// 按年份降序排序后搜索
const sortedBooks = books.sort((a, b) => b.year - a.year);
const recentBook = sortedBooks.find(book => book.year > 2018);
// { title: 'JavaScript Guide', year: 2020 }
实际应用示例
电商产品筛选功能实现:
class ProductFilter {
constructor(products) {
this.products = products;
}
sortByPrice(ascending = true) {
return this.products.sort((a, b) =>
ascending ? a.price - b.price : b.price - a.price
);
}
filterByCategory(category) {
return this.products.filter(
product => product.category === category
);
}
searchByName(keyword) {
return this.products.filter(product =>
product.name.toLowerCase().includes(keyword.toLowerCase())
);
}
}
// 使用示例
const products = [
{ name: 'Wireless Mouse', category: 'Electronics', price: 25 },
{ name: 'Bluetooth Speaker', category: 'Electronics', price: 50 },
{ name: 'Coffee Mug', category: 'Kitchen', price: 15 }
];
const filter = new ProductFilter(products);
const results = filter.sortByPrice()
.filterByCategory('Electronics')
.searchByName('wire');