0

I want to sort date from the product array (date is one of the field in product). Please suggest me. I am using ReactJS.

Please find the code that am using.

render() {

          let sortedDate = this.state.item.sort((a, b) => Date.parse(new Date(a.expected_ship_date("/").reverse().join("-"))) - Date.parse(new Date(b.expected_ship_date("/").reverse().join("-"))));
          let sortedDate1 = this.state.item.sort((a, b) => new Date(a.expected_ship_date) - new Date(b.expected_ship_date));

        return (
            <tr>
                <td><input name="line_number" type="text" value={ this.state.item.line_number } disabled={ this.props.mode }  className="form-control" onChange={ this.handleInputChange }/> </td>
                <td><input name="requested_ship_date" type="date" value={ this.state.item.expected_ship_date } disabled={ this.props.mode }  className="form-control" onChange={ this.handleInputChange }  /></td>

                <td>{sortedDate.map( (c, i) => <div>{c.expected_ship_date}</div>)}</td>
            </tr>

        );
}
2
  • Don't forget to slice() the array first, e.g. this.state.item.slice().sort(...), otherwise very bad things will start happening. Commented Jul 29, 2017 at 9:33
  • while sort date am getting this error: The specified value "[object Object]" does not conform to the required format, "yyyy-MM-dd". Please find the code ::::: for( var i=0;i < lines.length;i++) { for( var j=i+1; j< lines.length; j++) { if(lines[i].expected_ship_date < lines[j].expected_ship_date) { temp = lines[i].expected_ship_date; lines[i].expected_ship_date=lines[j].expected_ship_date; lines[j].expected_ship_date=temp; } } } this.state.item.order_ship_date=lines[0].expected_ship_date; Commented Jul 29, 2017 at 10:14

1 Answer 1

1

For sorting you need to convert the date to epoch date, and then try to sort. I suggest you should use moment to convert the date to epoch

Installation :

npm install --save moment

Code:

import moment from 'moment'

let sortedDate = this.state.item.sort((a, b) => moment(a.expected_ship_date).valueOf() - moment(b.expected_ship_date).valueOf());
Sign up to request clarification or add additional context in comments.

4 Comments

while console.log(sortedDate), its printing [object object]. am not getting the actual results. Please suggest.
Post the result console.log(this.state.item)
Actually, i have to get the latest date after i deleting a product from the table. removeLine(id) { var item = this.state.item; var lines = item.order_items; var sum=0; var total; var i = null; var count = 0 var temp = null; for (var j = 0; j < lines.length; j++) { if (lines[j].id == id) { i=j; } } lines.splice(i, 1); let sortedDate = lines.sort((a, b) => moment(a.expected_ship_date).valueOf() - moment(b.expected_ship_date).valueOf()); console.log("Final date:::::"+lines); this.setState({ item: item }); }
I think JavaScript sort function doesn't return anything. It sort the object itself. Don't require to store in other object.

Your Answer

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