C#,JS data sorting and its operation

Source: Internet
Author: User

List<int> listint=new list<int>{2,1,7,3,8,5,4,6};
Listint. Sort (x, y) = x-y);

var array = new Array ()
Array. sort (function (x, y) {return number (X[1])-Number (y[1]);});

JS array operation, some properties C # also have, do not enumerate

The addition of array elements

arrayObj. push([item1 [item2 [. . . [itemN ]]]]);// 将一个或多个新元素添加到数组结尾,并返回数组新长度

arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);// 将一个或多个新元素添加到数组开始,数组中的元素自动后移,返回数组新长度

arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]); //将一个或多个新元素插入到数组的指定位置,插入位置的元素自动后移,返回""。

Deletion of array elements

arrayObj.pop();  //移除最后一个元素并返回该元素值 arrayObj.shift();  //移除最前一个元素并返回该元素值,数组中元素自动前移 arrayObj.splice(deletePos,deleteCount);  //删除从指定位置deletePos开始的指定数量deleteCount的元素,数组形式返回所移除的元素

Interception and merging of arrays

arrayObj.slice(start, [end]);  //以数组的形式返回数组的一部分,注意不包括 end 对应的元素,如果省略 end 将复制 start 之后的所有元素 arrayObj.concat([item1[, item2[, . . . [,itemN]]]]);  //将多个数组(也可以是字符串,或者是数组和字符串的混合)连接为一个数组,返回连接好的新的数组

Copy of array

arrayObj.slice(0);  //返回数组的拷贝数组,注意是一个新的数组,不是指向 arrayObj.concat();  //返回数组的拷贝数组,注意是一个新的数组,不是指向

Ordering of array elements

arrayObj.reverse();  //反转元素(最前的排到最后、最后的排到最前),返回数组地址 arrayObj.sort();  //对数组元素排序,返回数组地址

string of array elements

arrayObj.join(separator);  //返回字符串,这个字符串将数组的每一个元素值连接在一起,中间 separator 隔开。toLocaleString, toString, valueOf: Can be seen as a special use of joins, not commonly used

Prototype property

Returns a reference to the object type prototype. The prototype property is common to object.

Objectname.prototype

The ObjectName parameter is the name of the object.

Description: Provides a basic set of functions for a class of objects using the prototype property. The new instance of the object "inherits" the action given to the object's prototype.

For array objects, use the following example to illustrate the purpose of the prototype property.

Adds a method to the array object that returns the value of the largest element in the array. To do this, declare a function, add it to array.prototype, and use it.

functionArray_max () {    varI,Max= This[0];     for(i = 1; i < This. length; i++)    {        if(Max < This[i])Max= This[i];    }    returnMax;}Array.prototype.max=Array_max;varx =NewArray (1, 2, 3, 4, 5, 6);vary = X.max ();

C#,JS data sorting and its operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.