javascript 排序_JavaScript中的排序方法

javascript 排序

There are tons of sorting algorithms available like bubble sort, merge sort, insertion sort etc. You must have implemented some of these in other programming languages like C or C++. But in this article, I will be demonstrating the Sorting methods inbuilt in JavaScript.

有大量的排序算法可用,例如冒泡排序,合并排序,插入排序等。您必须已经用其他编程语言(例如C或C ++)实现了其中一些。 但是在本文中,我将演示JavaScript内置Sorting方法

This is way different from usual sorting algorithms you must have seen.

这与您必须看到的常规排序算法不同。

JavaScript code:

JavaScript代码:

<html>
	<head><title>COLA PRODUCTS.!!!</title></head>
	<body>
		<script>
			document.write("<br>");
			var products = [ 
				{ name: "Grapefruit", calories: 170, color: "red", sold: 8200 },
				{ name: "Orange", calories: 160, color: "orange", sold: 12101 },
				{ name: "Cola", calories: 210, color: "caramel", sold: 25412 },
				{ name: "Diet Cola", calories: 0, color: "caramel", sold: 43922 },
				{ name: "Lemon", calories: 200, color: "clear", sold: 14983 },
				{ name: "Raspberry", calories: 180, color: "pink", sold: 9427 },
				{ name: "Root Beer", calories: 200, color: "caramel", sold: 9909 },
				{ name: "Water", calories: 0, color: "clear", sold: 62123 }
			];

			function compareSold(colaA, colaB) {
				if (colaA.sold > colaB.sold) {
					return 1;
				} else if (colaA.sold === colaB.sold) {
					return 0;
				} else {
					return -1;
				}
			}

			function compareName(colaA, colaB) {
				if (colaA.name > colaB.name) {
					return 1;
				} else if (colaA.name === colaB.name) {
					return 0;
				} else {
					return -1;
				}
			}

			function compareCalories(colaA, colaB) {
				if (colaA.calories > colaB.calories) {
					return 1;
				} else if (colaA.calories === colaB.calories) {
					return 0;
				} else {
					return -1;
				}
			}
			
			function compareColor(colaA, colaB) {
				if (colaA.color > colaB.color) {
					return 1;
				} else if (colaA.color === colaB.color) {
					return 0;
				} else {
					return -1;
				}
			}
			
			function printProducts(products) {
				for (var i = 0; i < products.length; i++) {
					document.write(" Name: " + products[i].name + "                                          "+
					"  Calories: " + products[i].calories +"                                          "+
					" Color: " + products[i].color + "                                          "+
					" Sold: " + products[i].sold);
					document.write("<br>");
				}
				document.write("<br>");
			}
			products.sort(compareSold);
			document.writeln("Products sorted by number of bottles sold:");
			document.write("<br>");
			printProducts(products);

			products.sort(compareName);
			document.write("Products sorted by name:");
			document.write("<br>");
			document.writeln();
			printProducts(products);
			products.sort(compareCalories);
			document.write("Products sorted by calories:");
			document.write("<br>");

			printProducts(products);
			products.sort(compareColor);
			document.write("Products sorted by color:");
			document.write("<br>");

			printProducts(products);
		</script>
	</body>
</html>


Output

输出量

Products sorted by number of bottles sold: 
Name: Grapefruit Calories: 170 Color: red Sold: 8200
Name: Raspberry Calories: 180 Color: pink Sold: 9427
Name: Root Beer Calories: 200 Color: caramel Sold: 9909
Name: Orange Calories: 160 Color: orange Sold: 12101
Name: Lemon Calories: 200 Color: clear Sold: 14983
Name: Cola Calories: 210 Color: caramel Sold: 25412
Name: Diet Cola Calories: 0 Color: caramel Sold: 43922
Name: Water Calories: 0 Color: clear Sold: 62123

Products sorted by name:
Name: Cola Calories: 210 Color: caramel Sold: 25412
Name: Diet Cola Calories: 0 Color: caramel Sold: 43922
Name: Grapefruit Calories: 170 Color: red Sold: 8200
Name: Lemon Calories: 200 Color: clear Sold: 14983
Name: Orange Calories: 160 Color: orange Sold: 12101
Name: Raspberry Calories: 180 Color: pink Sold: 9427
Name: Root Beer Calories: 200 Color: caramel Sold: 9909
Name: Water Calories: 0 Color: clear Sold: 62123

Products sorted by calories:
Name: Diet Cola Calories: 0 Color: caramel Sold: 43922
Name: Water Calories: 0 Color: clear Sold: 62123
Name: Orange Calories: 160 Color: orange Sold: 12101
Name: Grapefruit Calories: 170 Color: red Sold: 8200
Name: Raspberry Calories: 180 Color: pink Sold: 9427
Name: Lemon Calories: 200 Color: clear Sold: 14983
Name: Root Beer Calories: 200 Color: caramel Sold: 9909
Name: Cola Calories: 210 Color: caramel Sold: 25412

Products sorted by color:
Name: Diet Cola Calories: 0 Color: caramel Sold: 43922
Name: Root Beer Calories: 200 Color: caramel Sold: 9909
Name: Cola Calories: 210 Color: caramel Sold: 25412
Name: Water Calories: 0 Color: clear Sold: 62123
Name: Lemon Calories: 200 Color: clear Sold: 14983
Name: Orange Calories: 160 Color: orange Sold: 12101
Name: Raspberry Calories: 180 Color: pink Sold: 9427
Name: Grapefruit Calories: 170 Color: red Sold: 8200

Don't be scared...

别害怕...

The code is quite easy to understand.

该代码很容易理解。

First, an array of objects has been created. Each object having the same set of properties.

首先,已创建对象数组。 每个对象具有相同的属性集。

Arrays in JS have an inbuilt sort method. This sort method takes another comparing function defined by coders as an argument.

JS中的数组具有内置的排序方法。 这种排序方法采用编码器定义的另一个比较函数作为参数。

The major principle behind all this is that sort method needs a function to compare two elements of the array on which sort method is called. When that function is to be called and on which two elements of an array it is to be called that is decided by the sort method.

所有这些背后的主要原理是sort方法需要一个函数来比较调用sort方法的数组的两个元素。 当要调用该函数以及要在数组的哪两个元素上调用时,该函数由sort方法决定。

Here I have defined four functions that are used to compare two objects of an array at a time. Each function compares two objects on the different basis. Like function, compareName compares two objects on the basis of name property of objects.

在这里,我定义了四个函数,用于一次比较一个数组的两个对象。 每个函数在不同的基础上比较两个对象。 像函数一样,compareName根据对象的name属性比较两个对象。

If the name of the first object is lexicographically larger than the name of the second object then 1 is returned to sort method if they are equal 0 is returned and if the name of the first object is lexicographically smaller than the name of the second object then -1 is returned to sort method.

如果第一个对象的名称在字典上大于第二个对象的名称,则将1返回到sort方法,如果它们相等,则返回0,并且如果第一个对象的名称在字典上小于第二个对象的名称,则-1返回排序方法。

Other three functions compareSold, compareCaloriescompareColor work In exactly same manner.

其他三个函数compareSold , compareCaloriescompareColor以完全相同的方式工作。

Printproducts is a simple function used to print the input array.

Printproducts是用于打印输入数组的简单函数。

Document.write is like console.log which is used to display text on corresponding HTML page.

Document.write类似于console.log ,用于在相应HTML页面上显示文本。

I hope I have made everything very clear and precise.

我希望我已经使所有内容都非常清楚和准确了。

Try to make your own compare functions and use the inbuilt sort method on different arrays.

尝试制作自己的比较函数,并在不同的数组上使用内置的sort方法。

翻译自: https://www.includehelp.com/code-snippets/sorting-methods-in-javascript.aspx

javascript 排序

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值