Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
ordering sorting based multiple values

I have a list of objects where the object has the below variables.

BO={Name: String, Position: Number, SortOrder: Number};

Here SortOrder is just a number starting with 1 and increments by one for subsequent objects in an ascending order. The name and position values are provided and sort order needs to be populated. These are the rules to sort the list:

1.Sort the list based on the value of position
2.If the two objects have same position value then we need to sort the objects based on the Name in an alphanumeric order

I can use simple sorting based on the value of position by using a loop and using the below logic, but I need to be able to sort based on Name if the two positions have the same value.

for(i=0;i<Arr.length;i++)
    {
      for(j=i+1;j<Arr.length;j++)
        {
          if(Arr[i].poition>Arr[j].position)
            {
              temp=Arr[j];
              Arr[j]=Arr[i];
              Arr[i]=temp;
        }
      }
    }

let me know how this can be achieved
e.g.1
given :
[{Name:A,Position:2}
,{Name:B,Position:1},
{Name:C,Position:3}
]

Out put:
[{Name:B,Position:1,SortOrder:1},
{Name:A,Position:2,SortOrder:2},
{Name:C,Position:3,SortOrder:3}]

e.g.2
given
[ {Name:Ab,Position:2},
{Name:A,Position:2},
{Name:HI,Position:4},
{Name:C,Position:3}
]
Out put:
[{Name:A,Position:2,SortOrder:1}
, {Name:Ab,Position:2,SortOrder:2}
, {Name:C,Position:3,SortOrder:3},
{Name:HI,Position:4,SortOrder:4}]

I am thinking of using a for loop and javascript to implement this. Thank You.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could sort and map.

const
    data = [{ Name: 'Ab', Position: 2 }, { Name: 'A', Position: 2 }, { Name: 'HI', Position: 4 }, { Name: 'C', Position: 3 }],
    result = data
        .sort((a, b) => a.Position - b.Position || a.Name.localeCompare(b.Name))
        .map((o, i) => ({ ...o, SortOrder: i + 1 }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can sort based on Position and in case of same position sort alphabetically using string#localeCompare()

const sorter = (arr) => arr.sort((a,b) => a.Position - b.Position || a.Name.localeCompare(b.Name));

const arr1 = [{ Name: 'A', Position: 2 }, { Name: 'B', Position: 1 }, { Name: 'C', Position: 3 }];
const arr2 = [ {Name:'Ab',Position:2}, {Name:'A',Position:2}, {Name:'HI',Position:4}, {Name:'C',Position:3} ];

console.log(sorter(arr1));
console.log(sorter(arr2));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda