SHARE
TWEET

Untitled

a guest Aug 22nd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="description" content="How to Sort an Array of Objects in JavaScript [3]">
  6.   <meta name="viewport" content="width=device-width">
  7.   <title>JS Bin</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. 'use strict';
  13.  
  14. var cases = [{ referenceId: 999998563385,
  15.   mopId: 0,
  16.   collectionDate: '30/07/2019',
  17.   amountDue: '107.32',
  18.   amountReceived: 107.32,
  19.   status: 'Active',
  20.   versionNumber: 3,
  21.   receivingParentAccountNumber: 3000785361,
  22.   serviceType: 'Direct Pay',
  23.   childName: 'Takisha',
  24.   mopType: '--' }, { referenceId: 999998563361,
  25.   mopId: 0,
  26.   collectionDate: '31/07/2019',
  27.   amountDue: '64.55',
  28.   amountReceived: 64.55,
  29.   status: 'Active',
  30.   versionNumber: 3,
  31.   receivingParentAccountNumber: 3000785349,
  32.   serviceType: 'Direct Pay',
  33.   childName: 'Liane',
  34.   mopType: '--' }, { referenceId: 999998563384,
  35.   mopId: 0,
  36.   collectionDate: '06/08/2019',
  37.   amountDue: '107.32',
  38.   amountReceived: 107.32,
  39.   status: 'Active',
  40.   versionNumber: 3,
  41.   receivingParentAccountNumber: 3000785361,
  42.   serviceType: 'Direct Pay',
  43.   childName: 'Takisha',
  44.   mopType: '--' }];
  45. function compareValues(key) {
  46.   var order = arguments.length <= 1 || arguments[1] === undefined ? 'asc' : arguments[1];
  47.  
  48.   return function (a, b) {
  49.     if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
  50.       return 0;
  51.     }
  52.  
  53.     var varA = typeof a[key] === 'string' ? a[key].toUpperCase() : a[key];
  54.     var varB = typeof b[key] === 'string' ? b[key].toUpperCase() : b[key];
  55.  
  56.     var comparison = 0;
  57.     if (varA > varB) {
  58.       comparison = 1;
  59.     } else if (varA < varB) {
  60.       comparison = -1;
  61.     }
  62.     return order == 'desc' ? comparison * -1 : comparison;
  63.   };
  64. }
  65.  
  66. function datesSort(key) {
  67.   var order = arguments.length <= 1 || arguments[1] === undefined ? 'asc' : arguments[1];
  68.  
  69.   return function (a, b) {
  70.     if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
  71.       return 0;
  72.     }
  73.     var varA = a[key];
  74.     var varB = b[key];
  75.     console.log(varA, varB);
  76.     // '01/03/2014'.split('/')
  77.     // gives ["01", "03", "2014"]
  78.     x = varA.split('/');
  79.     y = varB.split('/');
  80.     var comparison = 0;
  81.     comparison = x[2] - y[2] || x[1] - y[1] || x[0] - y[0];
  82.     console.log(x[1], y[1], x[1] - y[1]);
  83.     console.log(comparison);
  84.     return order == 'desc' ? comparison * -1 : comparison;
  85.   };
  86. };
  87.  
  88. // cases.sort(datesSort('collectionDate', 'desc'))
  89.  
  90. console.log(cases.sort(datesSort('collectionDate', 'asc')));
  91. </script>
  92.  
  93.  
  94.  
  95. <script id="jsbin-source-javascript" type="text/javascript">const cases = [
  96.   { referenceId: 999998563385,
  97.     mopId: 0,
  98.     collectionDate: '30/07/2019',
  99.     amountDue: '107.32',
  100.     amountReceived: 107.32,
  101.     status: 'Active',
  102.     versionNumber: 3,
  103.     receivingParentAccountNumber: 3000785361,
  104.     serviceType: 'Direct Pay',
  105.     childName: 'Takisha',
  106.     mopType: '--' },
  107.   { referenceId: 999998563361,
  108.     mopId: 0,
  109.     collectionDate: '31/07/2019',
  110.     amountDue: '64.55',
  111.     amountReceived: 64.55,
  112.     status: 'Active',
  113.     versionNumber: 3,
  114.     receivingParentAccountNumber: 3000785349,
  115.     serviceType: 'Direct Pay',
  116.     childName: 'Liane',
  117.     mopType: '--' },
  118.   { referenceId: 999998563384,
  119.     mopId: 0,
  120.     collectionDate: '06/08/2019',
  121.     amountDue: '107.32',
  122.     amountReceived: 107.32,
  123.     status: 'Active',
  124.     versionNumber: 3,
  125.     receivingParentAccountNumber: 3000785361,
  126.     serviceType: 'Direct Pay',
  127.     childName: 'Takisha',
  128.     mopType: '--' }
  129. ];
  130. function compareValues(key, order='asc') {
  131.   return function(a, b) {
  132.     if(!a.hasOwnProperty(key) ||
  133.        !b.hasOwnProperty(key)) {
  134.       return 0;
  135.     }
  136.    
  137.     const varA = (typeof a[key] === 'string') ?
  138.       a[key].toUpperCase() : a[key];
  139.     const varB = (typeof b[key] === 'string') ?
  140.       b[key].toUpperCase() : b[key];
  141.      
  142.     let comparison = 0;
  143.     if (varA > varB) {
  144.       comparison = 1;
  145.     } else if (varA < varB) {
  146.       comparison = -1;
  147.     }
  148.     return (
  149.       (order == 'desc') ?
  150.       (comparison * -1) : comparison
  151.     );
  152.   };
  153. }
  154.  
  155. function datesSort(key, order='asc'){
  156.   return function(a, b){
  157.     if(!a.hasOwnProperty(key) ||
  158.        !b.hasOwnProperty(key)) {
  159.       return 0;
  160.     }
  161.     const varA = a[key];
  162.     const varB = b[key];
  163.     console.log(varA, varB)
  164.     // '01/03/2014'.split('/')
  165.     // gives ["01", "03", "2014"]
  166.     x = varA.split('/');
  167.     y = varB.split('/');
  168.     let comparison = 0;
  169.     comparison = x[2] - y[2] || x[1] - y[1] || x[0] - y[0];
  170.     console.log(x[1], y[1], x[1] - y[1])
  171.     console.log(comparison)
  172.     return (
  173.       (order == 'desc') ?
  174.       (comparison * -1) : comparison
  175.     );
  176.   };
  177. };
  178.  
  179. // cases.sort(datesSort('collectionDate', 'desc'))
  180.  
  181. console.log(
  182.   cases.sort(datesSort('collectionDate', 'asc'))
  183. );
  184. </script></body>
  185. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top