My data sort method for some reason is not working in my react app i.e return value is not changing
Consider this as my state and variables
state = {
percentageHighestOrLeast: "highest", //highest or lowest
townOrCounty: "town", //town or county
amountMaximumOrMinimum: "maximum" //maximum or minimum
}
Now In render I am checking if data is loaded without any
error and then calling a function
if (!this.props.mainListFetching && !this.props.mainListError) {
this.highestOrLeast = this.sortingData(this.props.mainList, this.state.percentageHighestOrLeast)
this.townOrCounty = this.sortingData(this.props.mainList, this.state.townOrCounty)
this.amountMaximumOrMinimum = this.sortingData(this.props.mainList, this.state.amountMaximumOrMinimum)
}
My this.sortinfData looks like this (this method is successfully being called)
sortingData = (data, type) => {
data.sort((a, b) => {
if (type == "highest") return (a["percentage.funded"] - b["percentage.funded"])
if (type == "lowest") return (b["percentage.funded"] - a["percentage.funded"])
if (type == "town") return (a["type"].localeCompare(b["type"]))
if (type == "county") return (b["type"].localeCompare(a["type"]))
if (type == "maximum") return (a["amt.pledged"] - b["amt.pledged"])
if (type == "minimum") return (b["amt.pledged"] - a["amt.pledged"])
})
return data
}
If i console.log this.highestOrLeast or 'this.amountMaximumOrMinimum' or this.townOrCounty, they all throw same result
This is how my data looks
[
{
"s.no": 0,
"amt.pledged": 15823,
"blurb": "'Catalysts, Explorers & Secret Keepers: Women of Science Fiction' is a take-home exhibit & anthology by the Museum of Science Fiction.",
"by": "Museum of Science Fiction",
"country": "US",
"currency": "usd",
"end.time": "2016-11-01T23:59:00-04:00",
"location": "Washington, DC",
"percentage.funded": 186,
"num.backers": "219382",
"state": "DC",
"title": "Catalysts, Explorers & Secret Keepers: Women of SF",
"type": "Town",
"url": "/projects/1608905146/catalysts-explorers-and-secret-keepers-women-of-sf?ref=discovery"
},
{
"s.no": 1,
"amt.pledged": 6859,
"blurb": "A unique handmade picture book for kids & art lovers about a nervous monster who finds his courage with the help of a brave little girl",
"by": "Tyrone Wells & Broken Eagle, LLC",
"country": "US",
"currency": "usd",
"end.time": "2016-11-25T01:13:33-05:00",
"location": "Portland, OR",
"percentage.funded": 8,
"num.backers": "154926",
"state": "OR",
"title": "The Whatamagump (a hand-crafted story picture book)",
"type": "Town",
"url"
[Question:] Can anyone help me figure out what I might be doing wrong here?
amt.pledged. What does the data really look like?(a.percentage.funded - b.percentage.funded)This is throwing an error saying Cannot read property 'funded' of undefined