1

An array contains the list of values as below:

    Object {status: "Pen"
    apptDate:"12-06-2001 04:00 PM "
    func:"OOS"}

  Object {status: "Pen"
    apptDate:"14-03-2001 04:00 PM "
    func:"OOS"}

    Object {status: "Pen"
    apptDate:"15-09-2001 04:00 PM "
    func:"OOS"}

 Object {status: "Pen"
    apptDate:"11-01-2001 04:00 PM "
    }

   Object {status: "Pen"
    apptDate:"10-02-2001 04:00 PM "
    }

Need to create a new array based on the current array that will have the object.func value as "OOS" should be pushed first inside the array while keeping the apptDate in asc order, incase no value present in object.func then sort the sort the array based on apptDate in asc order

3
  • I do not understand your question. Commented Aug 9, 2017 at 9:21
  • 2
    btw, 15-18-2001 is not a valid date. Commented Aug 9, 2017 at 9:23
  • need to create a new array based on the current array that will have the object.func value as "OOS" should be pushed first inside the array with keeping the apptDate in asc order, incase no value present in object.func then sort the sort the array based on apptDate in asc order Commented Aug 9, 2017 at 9:29

2 Answers 2

2

With an ISO 8601 date string, you could use String#localeCompare, while respecting 'OOS' sorting on top.

var array = [{ status: "Pen", apptDate: "2001-06-12 04:00", func: "OOS" }, { status: "Pen", apptDate: "2001-03-14 04:00", func: "OOS" }, { status: "Pen", apptDate: "2001-09-15 04:00", func: "OOS" }, { status: "Pen", apptDate: "2001-01-11 04:00" }, { status: "Pen", apptDate: "2001-02-10 04:00" }];

array.sort(function (a, b) {
    return (b.func === 'OOS') - (a.func === 'OOS') || a.apptDate.localeCompare(b.apptDate);
});

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

Sign up to request clarification or add additional context in comments.

4 Comments

You have an invalid date in input: 2001-06-12-06-2001
@NisargShah, well spotted.
@NinaScholz thanks for the response. what if i needs to sort on adding one more condition if status value is "pen" along with func === 'OOS' can we just use like below: return (b.func === 'OOS' ||b.status==='Pen') - (a.func === 'OOS'||b.status==='Pen') || a.apptDate.localeCompare(b.apptDate);
@Sangeethacg, actually all object have status: 'Pen'. should it sorted above func, ath the same place, or below of it? please add an extended example with more all the properties you want to sort. the properties should be different to demonstate the sorting result.
0

You can use array.sort with a comparing function such as this:

function(a,b){
    if(a.func && a.func == "OOS" && a.func != b.func){
    return -1;
  } else {
    return new Date(a.apptDate) - new Date(b.apptDate);
  }
}

Note: I've updated some dates to make them valid.

var arr = [{
  status: "Pen",
  apptDate: "12-18-2001 04:00 PM ",
  func: "OOS"
}, {
  status: "Pen",
  apptDate: "9-18-2001 04:00 PM ",
  func: "OOS"
}, {
  status: "Pen",
  apptDate: "11-18-2001 04:00 PM ",
  func: "OOS"
}, {
  status: "Pen",
  apptDate: "11-18-2001 04:00 PM "
}, {
  status: "Pen",
  apptDate: "10-18-2001 04:00 PM "
}];

arr.sort(function(a,b){
	if(a.func && a.func == "OOS" && a.func != b.func){
  	return -1;
  } else {
  	return new Date(a.apptDate) - new Date(b.apptDate);
  }
});

console.log(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.