Home > Software design >  Sort an array of objects based on regex match ignoring the missing items
Sort an array of objects based on regex match ignoring the missing items

Time:10-26

I have an array of objects called attachments where I need to sort based on sort_based_on_this_order array

If the sort_based_on_this_order array size is 7 items and array of objects size is 10 items. The array of objects has to be sorted in the order of 7 items followed by unspecified (missing) in last.

var sort_based_on_this_order = ['1666433101760', '1666434169913', '1666434194815', '1666432849037', '1666432764144', '1666432044571', '1666432742444']

document.write('<pre>'   JSON.stringify(
  attachments.sort((a, b) => {
    let k1 = a.filename.match(/\b[0-9]{13}(?=\.[^.] $)/)
    let k2 = b.filename.match(/\b[0-9]{13}(?=\.[^.] $)/)
    let i1 = sort_based_on_this_order.indexOf(k1[0])
    let i2 = sort_based_on_this_order.indexOf(k2[0])
    return i1 > i2 ? 1 : -1
  }), null, 4)   '</pre>')
<script>
var attachments = [{
  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978911/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666433926644.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978911
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978910/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666434169913.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978910
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978909/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666434048280.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978909
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978907/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666433101760.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978907
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978905/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666434194815.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978905
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978904/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432849037.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978904
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978903/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432801510.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978903
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978902/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432764144.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978902
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978901/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432044571.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978901
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978900/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432742444.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978900
}]
</script>

CodePudding user response:

If some item is missing,then you can assign it with an Infinity value

var attachments = [{
  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978911/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666433926644.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978911
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978910/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666434169913.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978910
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978909/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666434048280.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978909
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978907/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666433101760.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978907
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978905/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666434194815.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978905
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978904/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432849037.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978904
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978903/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432801510.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978903
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978902/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432764144.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978902
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978901/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432044571.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978901
}, {

  "download_small_url": "https://kf.kobotoolbox.org/api/v2/assets/ajDWYWQoe9Gw6WAnK8H4fG/data/194338021/attachments/82978900/",
  "mimetype": "image/jpeg",
  "filename": "deploymentsmaster/attachments/a46854ceb37c4e55b4a2c5ea565c4ebf/04ac3197-7d0f-4301-bbaf-68daaee2b1a8/1666432742444.jpg",
  "instance": 194338021,
  "xform": 740320,
  "id": 82978900
}]
var sort_based_on_this_order = ['1666433101760', '1666434169913', '1666434194815', '1666432849037', '1666432764144', '1666432044571', '1666432742444']

document.write('<pre>'   JSON.stringify(
  attachments.sort((a, b) => {
    let k1 = a.filename.match(/\b[0-9]{13}(?=\.[^.] $)/)
    let k2 = b.filename.match(/\b[0-9]{13}(?=\.[^.] $)/)
    let i1 = sort_based_on_this_order.indexOf(k1[0])
    let i2 = sort_based_on_this_order.indexOf(k2[0])
    if(i1==-1){
      i1 = Infinity
     }
    if(i2==-1){
      i2 = Infinity
     }
    return i1 > i2 ? 1 : -1
  }), null, 4)   '</pre>')

  • Related