John Doe John Doe - 1 year ago 68
Javascript Question

How to create an ordered array from values that have an order number?

So I have a series of strings, which contain and image path and order # concatenated.

They look like this:

const images = [

So the correct order should be: photo1, photo3, photo2.

What I need to do is process this into a correctly ordered array with just the path values. So ultimately I need:

const orderedImages = [

What is the best way to do this?

Answer Source

You can use array.sort to order them and to remove order value.

const images = [
var output = images.sort(function(a,b){
  return +a.split(',')[1] - +b.split(',')[1]
  return item.split(',')[0]


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download