Question: Hi I want to sort an array of objects in particular order. I am giving the array before sorting: 

```
var arrayBeforeSort = [
    {simulationID: '45679', colorName:'Green'},
    {simulationID: '45672', colorName:'red'},
    {simulationID: '45676', colorName:'yellow'},
    {simulationID: '45675', colorName:'Green'},
    {simulationID: '45677', colorName:'default'},
    {simulationID: '45673', colorName:'red'},
    {simulationID: '45680', colorName:'Green'},
    {simulationID: '45678', colorName:'default'}
]

```

I want to sort it like this:

```
var arrayAfterSort = [
    {simulationID: '45679', colorName:'Green'},
    {simulationID: '45675', colorName:'Green'},
    {simulationID: '45680', colorName:'Green'},
    {simulationID: '45672', colorName:'red'},
    {simulationID: '45673', colorName:'red'},
    {simulationID: '45676', colorName:'yellow'},
    {simulationID: '45677', colorName:'default'},
    {simulationID: '45678', colorName:'default'}
]

```

So basically I want to sort the array according to below order. Green will come first then Red will come then yellow then default. So sorting order will be **Green,Red,Yellow,Default**.

Answer: 
You can simply use `Arrays.sort()` and use an order array to sort the array in the order specified by the order array.

Try the following:

```js
var arrayBeforeSort = [ {simulationID: '45679', colorName:'Green'}, {simulationID: '45672', colorName:'red'}, {simulationID: '45676', colorName:'yellow'}, {simulationID: '45675', colorName:'Green'}, {simulationID: '45677', colorName:'default'}, {simulationID: '45673', colorName:'red'}, {simulationID: '45680', colorName:'Green'}, {simulationID: '45678', colorName:'default'} ];

var order = ["Green","red","yellow","default"];

arrayBeforeSort.sort((a,b)=> order.indexOf(a.colorName) - order.indexOf(b.colorName));

console.log(arrayBeforeSort);
```
First, define the value for the color based on your desired result. From there, you can compare them.

```
var colorArray = [];
colorArray['Green'] = 0;
colorArray['red'] = 1;
colorArray['yellow'] = 2;
colorArray['default'] = 3;
console.log(arrayBeforeSort.sort(function(a,b){
    return colorArray[a.colorName] > colorArray[b.colorName];
}));

```