博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 排序_JavaScript中的排序方法
阅读量:2530 次
发布时间:2019-05-11

本文共 4351 字,大约阅读时间需要 14 分钟。

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代码:

	COLA PRODUCTS.!!!				

Output

输出量

Products sorted by number of bottles sold: Name: Grapefruit Calories: 170 Color: red Sold: 8200Name: Raspberry Calories: 180 Color: pink Sold: 9427Name: Root Beer Calories: 200 Color: caramel Sold: 9909Name: Orange Calories: 160 Color: orange Sold: 12101Name: Lemon Calories: 200 Color: clear Sold: 14983Name: Cola Calories: 210 Color: caramel Sold: 25412Name: Diet Cola Calories: 0 Color: caramel Sold: 43922Name: Water Calories: 0 Color: clear Sold: 62123Products sorted by name:Name: Cola Calories: 210 Color: caramel Sold: 25412Name: Diet Cola Calories: 0 Color: caramel Sold: 43922Name: Grapefruit Calories: 170 Color: red Sold: 8200Name: Lemon Calories: 200 Color: clear Sold: 14983Name: Orange Calories: 160 Color: orange Sold: 12101Name: Raspberry Calories: 180 Color: pink Sold: 9427Name: Root Beer Calories: 200 Color: caramel Sold: 9909Name: Water Calories: 0 Color: clear Sold: 62123Products sorted by calories:Name: Diet Cola Calories: 0 Color: caramel Sold: 43922Name: Water Calories: 0 Color: clear Sold: 62123Name: Orange Calories: 160 Color: orange Sold: 12101Name: Grapefruit Calories: 170 Color: red Sold: 8200Name: Raspberry Calories: 180 Color: pink Sold: 9427Name: Lemon Calories: 200 Color: clear Sold: 14983Name: Root Beer Calories: 200 Color: caramel Sold: 9909Name: Cola Calories: 210 Color: caramel Sold: 25412Products sorted by color:Name: Diet Cola Calories: 0 Color: caramel Sold: 43922Name: Root Beer Calories: 200 Color: caramel Sold: 9909Name: Cola Calories: 210 Color: caramel Sold: 25412Name: Water Calories: 0 Color: clear Sold: 62123Name: Lemon Calories: 200 Color: clear Sold: 14983Name: Orange Calories: 160 Color: orange Sold: 12101Name: Raspberry Calories: 180 Color: pink Sold: 9427Name: 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方法。

翻译自:

javascript 排序

转载地址:http://hqxzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>