0

I have a json file called by fetch request that looks like this:

[
{
    "Infos": [
        {
            "id": {
                "Id": "105254"
            },
            "total": 142854739
        },
        {
            "id": {
                "Id": "105255"
            },
            "total": 112854739
        },
        {
            "id": {
                "Id": "105256"
            },
            "total": 132854739
        },
        {
            "id": {
                "Id": "106540"
            },
            "total": 122868818
        }
    ]
}
]

I want to sort data based on total field ,but as you can see all objects are in another array called Infos and I can not to sort data like this:

 Maindata.sort((a, b) => a.total - b.total);

How can I sort data based on total field that is in another an array?

     class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
        data: [],
        library: null,
        perPage: 20,
        currentPage: 1,
        maxPage: null,

    }
}
componentDidMount() {
    fetch('/json.bc', {
        method: 'get',
    })
        .then(response => response.text())
        .then(text => {
            let Maindata = JSON.parse(text.replace(/\'/g, '"'))
            Maindata.sort((a, b) => a.Infos[i].total - b.Infos[i].total) // I want to sort data here /////

            this.setState(state => ({
                ...state,
                data: Maindata
            }), () => {
                this.reorganiseLibrary()
            })
        }).catch(error => console.error(error))

}

reorganiseLibrary = () => {
    const { perPage, data } = this.state;
    let library = data;
    library = _.chunk(library, perPage);
    this.setState({
        library,
        currentPage: 1,
        maxPage: library.length === 0 ? 1 : library.length
    })
}



// Previous Page
previousPage = event => {
    this.setState({
        currentPage: this.state.currentPage - 1
    })
}
// Next Page 
nextPage = event => {
    this.setState({
        currentPage: this.state.currentPage + 1
    })
}

// handle per page
handlePerPage = (evt) =>
    this.setState({
        perPage: evt.target.value
    }, () => this.reorganiseLibrary());

// handle render of library
renderLibrary = () => {
    const { library, currentPage } = this.state;
    if (!library || (library && library.length === 0)) {
        return <div>NOResult</div>
    }
    return library[currentPage - 1].map((item, i) => (
        <div className="Wrapper">{this.renderInfo(item)}</div>
    ))
}

renderInfo(element){
    let len =element.Infos.length
    for (let i = 0; i < len; i++) {
      return element.Infos[i].total
    }

}

render() {
    const { library, currentPage, perPage, maxPage } = this.state;
    return (
        <div>
            {this.renderLibrary()}
            <ul id="page-numbers">
                <li className="nexprevPage">
                    {currentPage !== 1 && (
                        <button onClick={this.previousPage}><span className="fa-backward"></span></button>
                    )}
                </li>
                <li className="controlsPage activeCnt">{this.state.currentPage}</li>
                <li className="restControls">...</li>
                <li className="controlsPage">{this.state.maxPage}</li>
                <li className="nexprevPage">
                    {(currentPage < maxPage) && (
                        <button onClick={this.nextPage}><span className="fa-forward"></span></button>
                    )}
                </li>
            </ul>
        </div>
    )
}
}
ReactDOM.render(<App />, document.getElementById('Result'))

1 Answer 1

2

You can sort the inner field by mapping over the outer array and sorting the inner one like

Maindata = Maindata.map((data) => ({Infos: data.Infos.sort((a, b) => a.total - b.total)}))
Sign up to request clarification or add additional context in comments.

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.