1

Hello i need a better way to check if an array of property is Ascending sorted. i use the below The array can have dynamic length the property.

The array must be sorted using the DrawCD protperty i would like to do it in a better way without using a for cycle and if is possible using Array prototype function

pevDrawInfo = [{
    "drawCD": 16281,
    "drawTime": "14:55:00",
    "winCol": 2,
    "oddEven": 0
  },
  {
    "drawCD": 16280,
    "drawTime": "14:50:00",
    "winCol": 2,
    "oddEven": 0
  },
  {
    "drawCD": 16279,
    "drawTime": "14:45:00",
    "winCol": 2,
    "oddEven": 0
  },
  {
    "drawCD": 16278,
    "drawTime": "14:40:00",
    "winCol": 2,
    "oddEven": 0
  }
];



for (i = 0; i < 2; i++) {
  if (pevDrawInfo[i].drawCD - pevDrawInfo[i + 1].drawCD > 0)
    sorted = false
  else
    sorted = true
}


console.log(sorted)

3
  • "Better" how? Fewer lines of code? Fewer CPU cycles? Less memory? We need an objective measure that answers will be judged on, or else the question will be closed as "primarily opinion-based". Commented Apr 17, 2019 at 12:47
  • also if you need it sorted why not sort it just in case Commented Apr 17, 2019 at 12:48
  • i have update my request. i don't wont to sort it this must be return sorted from an API request and i need to check if is sorted or not Commented Apr 17, 2019 at 12:49

5 Answers 5

4

You could check with Array#every and take the property for checking.

var pevDrawInfo = [{ drawCD: 16281, drawTime: "14:55:00", winCol: 2, oddEven: 0 }, { drawCD: 16280, drawTime: "14:50:00", winCol: 2, oddEven: 0 }, { drawCD: 16279, drawTime: "14:45:00", winCol: 2, oddEven: 0 }, { drawCD: 16278, drawTime: "14:40:00", winCol: 2, oddEven: 0 }];

console.log(pevDrawInfo.every((b, i, { [i - 1]: a }) => !a || a.drawCD < b.drawCD));

pevDrawInfo.sort(({ drawCD: a }, { drawCD: b }) => a - b);

console.log(pevDrawInfo.every((b, i, { [i - 1]: a }) => !a || a.drawCD < b.drawCD));

Sign up to request clarification or add additional context in comments.

Comments

0

You can first map() array of object to array of required values. And then compare it with the sorted array.

var pevDrawInfo = [{ drawCD: 16281, drawTime: "14:55:00", winCol: 2, oddEven: 0 }, { drawCD: 16280, drawTime: "14:50:00", winCol: 2, oddEven: 0 }, { drawCD: 16279, drawTime: "14:45:00", winCol: 2, oddEven: 0 }, { drawCD: 16278, drawTime: "14:40:00", winCol: 2, oddEven: 0 }];

function isSorted(arr,key){
  arr = arr.map(x => x[key])
  return JSON.stringify(arr) === JSON.stringify(arr.sort((a,b) => b-a))
}

console.log(isSorted(pevDrawInfo,"drawCD"))

Comments

0

"better" usually means "reusable", "faster" or "more readable":

"reusable":

  • Abstract it into a function that takes an array and a key, and checks if every object's key property is ascending.

  • Make it work with a dynamic length, i < 2 won't work.

"faster"

  • Not sure if the code you have could be improved, it is already O(n) and does not use unneccessary variables / loops.

  • You could however exit early: If you found one entry that is descending, you don't have to check the rest.

"more readable"

looks good already (and better than the answers to be honest).

 function isAscending(array, key) {
   for (let i = 1; i < array.length; i++) {
     if (array[i - 1][key] - array[i][key] > 0)
      return false;
   }
   return true;
 }

"better way without using a for cycle"

Why? Does it make your code more readable? In this case: Definetly not (show Nina's answer to a coleague and ask him what it does). Because it is slower? No. Actually it is faster than abusing one of the Array helpers to do something they were not intended to.

Comments

0

You can use reduce function:

!!pevDrawInfo.reduce((n, item) => item.drawCD >= n.drawCD && item)

pevDrawInfo = [{
    "drawCD": 16281,
    "drawTime": "14:55:00",
    "winCol": 2,
    "oddEven": 0
  },
  {
    "drawCD": 16288,
    "drawTime": "14:50:00",
    "winCol": 2,
    "oddEven": 0
  },
  {
    "drawCD": 16288,
    "drawTime": "14:45:00",
    "winCol": 2,
    "oddEven": 0
  },
  {
    "drawCD": 16298,
    "drawTime": "14:40:00",
    "winCol": 2,
    "oddEven": 0
  }
];

console.log(!!pevDrawInfo.reduce((n, item) => item.drawCD >= n.drawCD && item))

Comments

0

my way... ( more readable )

var
  pevDrawInfo = [
    { "drawCD": 16281, "drawTime": "14:55:00", "winCol": 2, "oddEven": 0 }
  , { "drawCD": 16280, "drawTime": "14:50:00", "winCol": 2, "oddEven": 0 }
  , { "drawCD": 16279, "drawTime": "14:45:00", "winCol": 2, "oddEven": 0 }
  , { "drawCD": 16278, "drawTime": "14:40:00", "winCol": 2, "oddEven": 0 }
  ];

var sorted = true;

pevDrawInfo.reduce((a,c)=>{
  sorted = sorted && (a.drawCD > c.drawCD); //  strict descending order (change to >=, < or <=..)
  return c;
}) 

console.log( 'strict descending order :', sorted )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.