Guest User

Untitled

a guest
Dec 9th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. ### Sort object properties by value (values are text)
  2. I have following object:
  3. ```js
  4. var cities={10:'Tashkent', 14:'Karakalpakiya', 16:'Andijan'};
  5. ```
  6. I want sort it by city names, so after sort it should be:
  7. ```js
  8. var cities={16:'Andijan', 14:'Karakalpakiya', 10:'Tashkent'};
  9. ```
  10. But I can't sort object properties, instead can convert object into array, then sort items.
  11. ```js
  12. function sortProperties(obj)
  13. {
  14. // convert object into array
  15. var sortable=[];
  16. for(var key in obj)
  17. if(obj.hasOwnProperty(key))
  18. sortable.push([key, obj[key]]); // each item is an array in format [key, value]
  19.  
  20. // sort items by value
  21. sortable.sort(function(a, b)
  22. {
  23. var x=a[1].toLowerCase(),
  24. y=b[1].toLowerCase();
  25. return x<y ? -1 : x>y ? 1 : 0;
  26. });
  27. return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
  28. }
  29. ```
  30.  
  31. Example:
  32. ```js
  33. var sortedCities=sortProperties(cities);
  34. console.log(sortedCities); // [[16, 'Andijan'], [14, 'Karakalpakiya'], [10, 'Tashkent']]
  35. ```
  36. ### Sort object properties by value (values are numbers)
  37. I have following object:
  38. ```js
  39. var cities={Tashkent:447, Karakalpakiya:900, Andijan:120};
  40. ```
  41. I want sort it by city rating, so after sort it should be:
  42. ```js
  43. var cities={Andijan:120, Tashkent:447, Karakalpakiya:900};
  44. ```
  45. But again I can't sort object properties, instead can convert object into array, then sort items.
  46. ```js
  47. function sortProperties(obj)
  48. {
  49. // convert object into array
  50. var sortable=[];
  51. for(var key in obj)
  52. if(obj.hasOwnProperty(key))
  53. sortable.push([key, obj[key]]); // each item is an array in format [key, value]
  54.  
  55. // sort items by value
  56. sortable.sort(function(a, b)
  57. {
  58. return a[1]-b[1]; // compare numbers
  59. });
  60. return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
  61. }
  62. ```
  63. Example:
  64. ```js
  65. var sortedCities=sortProperties(cities);
  66. console.log(sortedCities); // [[Andijan, 120], [Tashkent, 447], [Karakalpakiya, 900]]
  67. ```
  68. ###Improved version with additional parameter to set sort type (numeric or text)
  69. ```js
  70. /**
  71. * Sort object properties (only own properties will be sorted).
  72. * @param {object} obj object to sort properties
  73. * @param {bool} isNumericSort true - sort object properties as numeric value, false - sort as string value.
  74. * @returns {Array} array of items in [[key,value],[key,value],...] format.
  75. */
  76. function sortProperties(obj, isNumericSort)
  77. {
  78. isNumericSort=isNumericSort || false; // by default text sort
  79. var sortable=[];
  80. for(var key in obj)
  81. if(obj.hasOwnProperty(key))
  82. sortable.push([key, obj[key]]);
  83. if(isNumericSort)
  84. sortable.sort(function(a, b)
  85. {
  86. return a[1]-b[1];
  87. });
  88. else
  89. sortable.sort(function(a, b)
  90. {
  91. var x=a[1].toLowerCase(),
  92. y=b[1].toLowerCase();
  93. return x<y ? -1 : x>y ? 1 : 0;
  94. });
  95. return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
  96. }
  97. ```
Add Comment
Please, Sign In to add comment