Tengo un objeto con múltiples propiedades y quiero ordenar según una lista de enumeración específica.
A continuación se muestra el objeto
const ArratOfobj = [ { id: 3, ShortType: "LocationWorn", ImageType: "B" }, { id: 2, ShortType: "SipStillLife", ImageType: "D" }, { id: 1, ShortType: "SipStillWorn", ImageType: "M" }, { id: 4, ShortType: "LocationLife", ImageType: "M" }, ];Orden de enumeración
const order= { '0': { ShortType: "SIP Still Life", ImageType: "M"}, '1': { ShortType: "SIP Still Life", ImageType: "B" }, '2': { ShortType: "Location Still Life", ImageType: "M" }, '3': { ShortType: "Location Still Life", ImageType: "B" } }**Rendimiento esperado **
[ { id: 2, ImageType: "D", ShortType: "SipStillLife" }, { id: 4, ImageType: "M", ShortType: "LocationLife" }, { id: 1, ImageType: "M", ShortType: "SipStillWorn" }, { id: 3, ImageType: "B", ShortType: "LocationWorn" } ]no puedes crear un objeto de esta manera, pero supongo que te refieres a una matriz de objetos ob.
así es como podría ordenar esta matriz usando el orden de enumeración:
const ArratOfobj = [ { id: 3, ShortType: "LocationWorn", ImageType: "B" }, { id: 2, ShortType: "SipStillLife", ImageType: "D" }, { id: 1, ShortType: "SipStillWorn", ImageType: "M" }, { id: 4, ShortType: "LocationLife", ImageType: "M" }, ]; const enum Order { SipStillLife = 0, LocationLife = 1, SipStillWorn = 2, LocationWorn = 3, } const sortedArray = ArratOfobj.sort((a, b) => { return Order[a.ShortType] - Order[b.ShortType]; });