JavaScript数字数组怎么按数子大小排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript数字数组怎么按数子大小排序相关的知识,希望对你有一定的参考价值。

你好,javascript中的数字数组排序非常简单,JS本身提供了内置的排序方法,直接调用就可以了。

var arr = [1, 20, 49, 32, 43, 69];
arr.sort();
console.log(arr); // [1, 20, 32, 43, 49, 69]

还有一种更加灵活的写法:

arr.sort(function(a, b)
    return a - b;
);
console.log(arr); // [1, 20, 32, 43, 49, 69]

如果想倒序也很简单,交换下return中a、b的位置就可以了:

arr.sort(function(a, b)
    return b - a;
);
console.log(arr); // [69, 49, 43, 32, 20, 1]

好了,就这样。希望是你想要的答案,望采纳,如有疑问请追问!

参考技术A

    先写上我们的示例代码,定义了一个数组arr,数组包括有几个字母,不按顺序的,要对数组进行排序,直接调用sort方法。再加上一些输出语句,console.log是指在浏览器的调试控制台里输出内容。

    运行页面,我们在控制台里看下结果,如图,数组调用sort方法后,就按字母的升序做好排序了。

    修改下代码,把数组内容从字符串改成数字,然后再调用sort方法。

    修改后,运行页面,再看下结果。

    数组排序都是按字符串来排序的,而不管数组内容是数字还是字符串。

    修改下sort排序方法,把刚才定义的方法名传进来。

    运行页面,可以看到现在的数组就是按数字从小到大排序的。

好,Javascript中的数字数组排序非常简单,JS本身提供了内置的排序方法,直接调用就可以了。

1.var arr = [1, 20, 49, 32, 43, 69];

2.arr.sort();

3.console.log(arr); // [1, 20, 32, 43, 49, 69]

参考技术B var a=[1,2,6,3,10];a.sort(function(a,b)return a-b) 参考技术C 方法一:sort()
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b)return a - b);
方法2:经典冒泡排序
var arr = [1, 3, 2, 9, 8, 15, 12, 18, 5];
var t = 0;
for (var i = 0; i < arr.length - 1; i++)
for (var j = 0; j < arr.length - i - 1; j++)
if (arr[j] > arr[j + 1])
t = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = t;



console.log(arr)

mapreduce 实现数子排序

设计思路:

  使用mapreduce的默认排序,按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String的Text类型,那么MapReduce按照字典顺序对字符串排序。

  首先map阶段将输入的数字作为key,  并记录相同key出现的次数,在reduce阶段将输入的key作为输出的value,如果相同值存在多个,循环便利输出。

 

源数据:file1

2
32
654
32
15
756
65223

  file2

5956
22
650
92

  file3

26
54
6
2
15

  源代码

package com.duking.hadoop;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class DataSort {

	public static class Map extends
			Mapper<Object, Text, IntWritable, IntWritable> {

		private static IntWritable data = new IntWritable();

		// 实现map函数
		public void map(Object key, Text value, Context context)

		throws IOException, InterruptedException {
			
			String line = value.toString(); // 将输入的每一行数据转换为String类型

			data.set(Integer.parseInt(line)); // 将String 转换为Integer

			context.write(data, new IntWritable(1)); // 将 date->key
														// 统计key出现的次数自增为value
		}
	}

	// reduce将输入中的key复制到输出数据的key上,并直接输出 这是数据区重的思想
	public static class Reduce extends
			Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {

		private static IntWritable linenum = new IntWritable(1);
		private IntWritable result = new IntWritable();

		// 实现reduce函数

		public void reduce(IntWritable key, Iterable<IntWritable> values,  //Iterable转为List
				Context context)

		throws IOException, InterruptedException {
			
			for (IntWritable val : values) {

				context.write(linenum, key);

				linenum = new IntWritable(linenum.get() + 1);
			}
		}

	}

	public static void main(String[] args) throws Exception {

		Configuration conf = new Configuration();

		conf.set("mapred.job.tracker", "192.168.60.129:9000");

		// 指定带运行参数的目录为输入输出目录
		String[] otherArgs = new GenericOptionsParser(conf, args)
				.getRemainingArgs();

		/*
		 * 指定工程下的input2为文件输入目录 output2为文件输出目录 String[] ioArgs = new String[] {
		 * "input2", "output2" };
		 * 
		 * String[] otherArgs = new GenericOptionsParser(conf, ioArgs)
		 * .getRemainingArgs();
		 */

		if (otherArgs.length != 2) { // 判断路径参数是否为2个

			System.err.println("Usage: Data Deduplication <in> <out>");

			System.exit(2);

		}

		// set maprduce job name
		Job job = new Job(conf, "Data sort");

		job.setJarByClass(DataSort.class);

		// 设置Map、Combine和Reduce处理类

		job.setMapperClass(Map.class);

		job.setCombinerClass(Reduce.class);

		job.setReducerClass(Reduce.class);

		// 设置输出类型

		job.setOutputKeyClass(IntWritable.class);

		job.setOutputValueClass(IntWritable.class);

		// 设置输入和输出目录

		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));

		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

		System.exit(job.waitForCompletion(true) ? 0 : 1);

	}

}

  结果

1	2
2	2
3	6
4	15
5	15
6	22
7	26
8	32
9	32
10	54
11	92
12	650
13	654
14	756
15	5956
16	65223

  

  

以上是关于JavaScript数字数组怎么按数子大小排序的主要内容,如果未能解决你的问题,请参考以下文章

mapreduce 实现数子排序

js sort 数组中包含数组怎么排序

排序算法

java怎么让数组的数字从大到小排序?

java怎么让数组的数字从大到小排序?

数组中数字出现的次数