Sort Json data by Field Based on JavaScript _ javascript skills

Source: Internet
Author: User
This article describes how to sort Json data by a Field Based on JavaScript. For more information, see I. first introduce the built-in sort () method in js.

By default, this method sorts the elements in the array in alphabetical order. More precise, the elements are sorted in character encoding order.

Take the following example:

When the elements in the array are numeric, the sorting result is completely different from what we imagine, because the sorting is performed by default in the character encoding order.

Solution: the sort () method receives an optional parameter (this parameter must be a function). We can define the sorting rules by ourselves, as shown in figure

II. Specific implementation of json sorting

/** @ Description sorts the json array Based on a field * @ param array the json array object to be sorted * @ param field sorting field (this parameter must be a string) * @ param reverse order (false by default) * @ return array returns the sorted json array */function jsonSort (array, field, reverse) {// The array length is less than 2 or no sorting field is specified or if (array. length <2 |! Field | typeof array [0]! = "Object") return array; // number type sorting if (typeof array [0] [field] = "number") {array. sort (function (x, y) {return x [field]-y [field]});} // string type sorting if (typeof array [0] [field] = "string") {array. sort (function (x, y) {return x [field]. localeCompare (y [field])});} // reverse order if (reverse) {array. reverse ();} return array ;}

PS: JS: json object array sorted by object attribute

var array = [  {name: 'a', phone: 1},  {name: 'b', phone: 5},  {name: 'd', phone: 3},  {name: 'c', phone: 4}]array.sort(getSortFun('desc', 'phone'));function getSortFun(order, sortBy) {  var ordAlpah = (order == 'asc') ? '>' : '<';  var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1');  return sortFun;}alert(JSON.stringify(array));

The array itself has the sort method, which can be used to specify the sorting function. Therefore, a sorting function can be dynamically generated to sort by specified object attributes;

Note: The original array sequence will change after sort !!

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.