Sorting React table by clicking table headers for columns with numerical and string values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am having problems in sorting my React table by clicking on columns headers for column with string values, but with the column with numerical values it's all ok. I think that I am doing wrong in sortBySymbolHandler method. I suppose that my check for strings: this.state.directionSymbol[key] === 'asc'? a[key] - b[key] : b[key] - a[key] is wrong.
import React, { Component } from 'react';
import CoinTable from '../components/CoinTable/CoinTable';
import data from '../components/Data/Data.json';
class App extends Component {
constructor(props) {
super(props)
this.state = {
data: data,
direction: {
price_usd: 'asc'
},
directionSymbol: {
symbol: 'asc'
}
}
}
sortBySymbolHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
directionSymbol: {
[key]: this.state.directionSymbol[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
sortByPriceHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.direction[key] === 'asc'
?
parseFloat(a[key]) - parseFloat(b[key])
: parseFloat(b[key]) - parseFloat(a[key])
)),
direction: {
[key]: this.state.direction[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
render() {
return (
<div className="App">
<div className="container-fluid">
<div className="container">
<CoinTable
data={this.state.data}
sortBySymbol={this.sortBySymbolHandler}
sortByPrice={this.sortByPriceHandler}
/>
</div>
</div>
</div>
);
}
}
export default App;
Data.json is in this format:
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "10406.5",
"price_btc": "1.0",
"24h_volume_usd": "9766700000.0",
"market_cap_usd": "175053324790",
"available_supply": "16821537.0",
"total_supply": "16821537.0",
"max_supply": "21000000.0",
"percent_change_1h": "-0.04",
"percent_change_24h": "-3.39",
"percent_change_7d": "-12.62",
"last_updated": "1516718960"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "947.841",
"price_btc": "0.0917641",
"24h_volume_usd": "3615420000.0",
"market_cap_usd": "92093645501.0",
"available_supply": "97161492.0",
"total_supply": "97161492.0",
"max_supply": null,
"percent_change_1h": "0.3",
"percent_change_24h": "-4.04",
"percent_change_7d": "-13.22",
"last_updated": "1516718952"
},
{
"id": "ripple",
"name": "Ripple",
"symbol": "XRP",
"rank": "3",
"price_usd": "1.27202",
"price_btc": "0.00012315",
"24h_volume_usd": "2597960000.0",
"market_cap_usd": "49276964438.0",
"available_supply": "38739142811.0",
"total_supply": "99993093880.0",
"max_supply": "100000000000",
"percent_change_1h": "0.83",
"percent_change_24h": "3.49",
"percent_change_7d": "-5.9",
"last_updated": "1516718941"
}
]
and CoinTable.js is like this:
import React from 'react';;
import TableRow from './TableRow/TableRow';
const coinTable = (props) => (
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th><button onClick={() => props.sortBySymbol('symbol')}>Symbol</button></th>
<th><button onClick={() => props.sortByPrice('price_usd')}>Price</button></th>
<th>%/hour</th>
<th>%/day</th>
<th>%/week</th>
</tr>
</thead>
<tbody>
{
props.data.map(row => {
const price = row.price_usd;
const formattedPrice = parseFloat(price).toFixed(2);
return (
<TableRow key={row.rank} row={row} formattedPrice={formattedPrice} />
)
})
}
</tbody>
</table>
);
export default coinTable;
Can somebody help?
javascript reactjs
add a comment |
I am having problems in sorting my React table by clicking on columns headers for column with string values, but with the column with numerical values it's all ok. I think that I am doing wrong in sortBySymbolHandler method. I suppose that my check for strings: this.state.directionSymbol[key] === 'asc'? a[key] - b[key] : b[key] - a[key] is wrong.
import React, { Component } from 'react';
import CoinTable from '../components/CoinTable/CoinTable';
import data from '../components/Data/Data.json';
class App extends Component {
constructor(props) {
super(props)
this.state = {
data: data,
direction: {
price_usd: 'asc'
},
directionSymbol: {
symbol: 'asc'
}
}
}
sortBySymbolHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
directionSymbol: {
[key]: this.state.directionSymbol[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
sortByPriceHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.direction[key] === 'asc'
?
parseFloat(a[key]) - parseFloat(b[key])
: parseFloat(b[key]) - parseFloat(a[key])
)),
direction: {
[key]: this.state.direction[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
render() {
return (
<div className="App">
<div className="container-fluid">
<div className="container">
<CoinTable
data={this.state.data}
sortBySymbol={this.sortBySymbolHandler}
sortByPrice={this.sortByPriceHandler}
/>
</div>
</div>
</div>
);
}
}
export default App;
Data.json is in this format:
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "10406.5",
"price_btc": "1.0",
"24h_volume_usd": "9766700000.0",
"market_cap_usd": "175053324790",
"available_supply": "16821537.0",
"total_supply": "16821537.0",
"max_supply": "21000000.0",
"percent_change_1h": "-0.04",
"percent_change_24h": "-3.39",
"percent_change_7d": "-12.62",
"last_updated": "1516718960"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "947.841",
"price_btc": "0.0917641",
"24h_volume_usd": "3615420000.0",
"market_cap_usd": "92093645501.0",
"available_supply": "97161492.0",
"total_supply": "97161492.0",
"max_supply": null,
"percent_change_1h": "0.3",
"percent_change_24h": "-4.04",
"percent_change_7d": "-13.22",
"last_updated": "1516718952"
},
{
"id": "ripple",
"name": "Ripple",
"symbol": "XRP",
"rank": "3",
"price_usd": "1.27202",
"price_btc": "0.00012315",
"24h_volume_usd": "2597960000.0",
"market_cap_usd": "49276964438.0",
"available_supply": "38739142811.0",
"total_supply": "99993093880.0",
"max_supply": "100000000000",
"percent_change_1h": "0.83",
"percent_change_24h": "3.49",
"percent_change_7d": "-5.9",
"last_updated": "1516718941"
}
]
and CoinTable.js is like this:
import React from 'react';;
import TableRow from './TableRow/TableRow';
const coinTable = (props) => (
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th><button onClick={() => props.sortBySymbol('symbol')}>Symbol</button></th>
<th><button onClick={() => props.sortByPrice('price_usd')}>Price</button></th>
<th>%/hour</th>
<th>%/day</th>
<th>%/week</th>
</tr>
</thead>
<tbody>
{
props.data.map(row => {
const price = row.price_usd;
const formattedPrice = parseFloat(price).toFixed(2);
return (
<TableRow key={row.rank} row={row} formattedPrice={formattedPrice} />
)
})
}
</tbody>
</table>
);
export default coinTable;
Can somebody help?
javascript reactjs
add a comment |
I am having problems in sorting my React table by clicking on columns headers for column with string values, but with the column with numerical values it's all ok. I think that I am doing wrong in sortBySymbolHandler method. I suppose that my check for strings: this.state.directionSymbol[key] === 'asc'? a[key] - b[key] : b[key] - a[key] is wrong.
import React, { Component } from 'react';
import CoinTable from '../components/CoinTable/CoinTable';
import data from '../components/Data/Data.json';
class App extends Component {
constructor(props) {
super(props)
this.state = {
data: data,
direction: {
price_usd: 'asc'
},
directionSymbol: {
symbol: 'asc'
}
}
}
sortBySymbolHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
directionSymbol: {
[key]: this.state.directionSymbol[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
sortByPriceHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.direction[key] === 'asc'
?
parseFloat(a[key]) - parseFloat(b[key])
: parseFloat(b[key]) - parseFloat(a[key])
)),
direction: {
[key]: this.state.direction[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
render() {
return (
<div className="App">
<div className="container-fluid">
<div className="container">
<CoinTable
data={this.state.data}
sortBySymbol={this.sortBySymbolHandler}
sortByPrice={this.sortByPriceHandler}
/>
</div>
</div>
</div>
);
}
}
export default App;
Data.json is in this format:
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "10406.5",
"price_btc": "1.0",
"24h_volume_usd": "9766700000.0",
"market_cap_usd": "175053324790",
"available_supply": "16821537.0",
"total_supply": "16821537.0",
"max_supply": "21000000.0",
"percent_change_1h": "-0.04",
"percent_change_24h": "-3.39",
"percent_change_7d": "-12.62",
"last_updated": "1516718960"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "947.841",
"price_btc": "0.0917641",
"24h_volume_usd": "3615420000.0",
"market_cap_usd": "92093645501.0",
"available_supply": "97161492.0",
"total_supply": "97161492.0",
"max_supply": null,
"percent_change_1h": "0.3",
"percent_change_24h": "-4.04",
"percent_change_7d": "-13.22",
"last_updated": "1516718952"
},
{
"id": "ripple",
"name": "Ripple",
"symbol": "XRP",
"rank": "3",
"price_usd": "1.27202",
"price_btc": "0.00012315",
"24h_volume_usd": "2597960000.0",
"market_cap_usd": "49276964438.0",
"available_supply": "38739142811.0",
"total_supply": "99993093880.0",
"max_supply": "100000000000",
"percent_change_1h": "0.83",
"percent_change_24h": "3.49",
"percent_change_7d": "-5.9",
"last_updated": "1516718941"
}
]
and CoinTable.js is like this:
import React from 'react';;
import TableRow from './TableRow/TableRow';
const coinTable = (props) => (
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th><button onClick={() => props.sortBySymbol('symbol')}>Symbol</button></th>
<th><button onClick={() => props.sortByPrice('price_usd')}>Price</button></th>
<th>%/hour</th>
<th>%/day</th>
<th>%/week</th>
</tr>
</thead>
<tbody>
{
props.data.map(row => {
const price = row.price_usd;
const formattedPrice = parseFloat(price).toFixed(2);
return (
<TableRow key={row.rank} row={row} formattedPrice={formattedPrice} />
)
})
}
</tbody>
</table>
);
export default coinTable;
Can somebody help?
javascript reactjs
I am having problems in sorting my React table by clicking on columns headers for column with string values, but with the column with numerical values it's all ok. I think that I am doing wrong in sortBySymbolHandler method. I suppose that my check for strings: this.state.directionSymbol[key] === 'asc'? a[key] - b[key] : b[key] - a[key] is wrong.
import React, { Component } from 'react';
import CoinTable from '../components/CoinTable/CoinTable';
import data from '../components/Data/Data.json';
class App extends Component {
constructor(props) {
super(props)
this.state = {
data: data,
direction: {
price_usd: 'asc'
},
directionSymbol: {
symbol: 'asc'
}
}
}
sortBySymbolHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
directionSymbol: {
[key]: this.state.directionSymbol[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
sortByPriceHandler = (key) => {
this.setState({
data: data.sort((a, b) => (
this.state.direction[key] === 'asc'
?
parseFloat(a[key]) - parseFloat(b[key])
: parseFloat(b[key]) - parseFloat(a[key])
)),
direction: {
[key]: this.state.direction[key] === 'asc'
? 'desc'
: 'asc'
}
})
}
render() {
return (
<div className="App">
<div className="container-fluid">
<div className="container">
<CoinTable
data={this.state.data}
sortBySymbol={this.sortBySymbolHandler}
sortByPrice={this.sortByPriceHandler}
/>
</div>
</div>
</div>
);
}
}
export default App;
Data.json is in this format:
[
{
"id": "bitcoin",
"name": "Bitcoin",
"symbol": "BTC",
"rank": "1",
"price_usd": "10406.5",
"price_btc": "1.0",
"24h_volume_usd": "9766700000.0",
"market_cap_usd": "175053324790",
"available_supply": "16821537.0",
"total_supply": "16821537.0",
"max_supply": "21000000.0",
"percent_change_1h": "-0.04",
"percent_change_24h": "-3.39",
"percent_change_7d": "-12.62",
"last_updated": "1516718960"
},
{
"id": "ethereum",
"name": "Ethereum",
"symbol": "ETH",
"rank": "2",
"price_usd": "947.841",
"price_btc": "0.0917641",
"24h_volume_usd": "3615420000.0",
"market_cap_usd": "92093645501.0",
"available_supply": "97161492.0",
"total_supply": "97161492.0",
"max_supply": null,
"percent_change_1h": "0.3",
"percent_change_24h": "-4.04",
"percent_change_7d": "-13.22",
"last_updated": "1516718952"
},
{
"id": "ripple",
"name": "Ripple",
"symbol": "XRP",
"rank": "3",
"price_usd": "1.27202",
"price_btc": "0.00012315",
"24h_volume_usd": "2597960000.0",
"market_cap_usd": "49276964438.0",
"available_supply": "38739142811.0",
"total_supply": "99993093880.0",
"max_supply": "100000000000",
"percent_change_1h": "0.83",
"percent_change_24h": "3.49",
"percent_change_7d": "-5.9",
"last_updated": "1516718941"
}
]
and CoinTable.js is like this:
import React from 'react';;
import TableRow from './TableRow/TableRow';
const coinTable = (props) => (
<table>
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th><button onClick={() => props.sortBySymbol('symbol')}>Symbol</button></th>
<th><button onClick={() => props.sortByPrice('price_usd')}>Price</button></th>
<th>%/hour</th>
<th>%/day</th>
<th>%/week</th>
</tr>
</thead>
<tbody>
{
props.data.map(row => {
const price = row.price_usd;
const formattedPrice = parseFloat(price).toFixed(2);
return (
<TableRow key={row.rank} row={row} formattedPrice={formattedPrice} />
)
})
}
</tbody>
</table>
);
export default coinTable;
Can somebody help?
javascript reactjs
javascript reactjs
asked Nov 16 '18 at 20:50
IvanaIvana
657
657
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can't subtract one string from another string.
Instead of doing:
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
Do:
data: data.sort((a, b) => {
const asc = this.state.directionSymbol[key] === 'asc';
if (a[key] < b[key]) {
return asc ? -1 : 1;
} else if (a[key] > b[key]) {
return asc ? 1 : -1;
} else {
return 0;
}
)),
Thanks Doug, it was helpfull.
– Ivana
Nov 16 '18 at 21:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345196%2fsorting-react-table-by-clicking-table-headers-for-columns-with-numerical-and-str%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't subtract one string from another string.
Instead of doing:
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
Do:
data: data.sort((a, b) => {
const asc = this.state.directionSymbol[key] === 'asc';
if (a[key] < b[key]) {
return asc ? -1 : 1;
} else if (a[key] > b[key]) {
return asc ? 1 : -1;
} else {
return 0;
}
)),
Thanks Doug, it was helpfull.
– Ivana
Nov 16 '18 at 21:26
add a comment |
You can't subtract one string from another string.
Instead of doing:
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
Do:
data: data.sort((a, b) => {
const asc = this.state.directionSymbol[key] === 'asc';
if (a[key] < b[key]) {
return asc ? -1 : 1;
} else if (a[key] > b[key]) {
return asc ? 1 : -1;
} else {
return 0;
}
)),
Thanks Doug, it was helpfull.
– Ivana
Nov 16 '18 at 21:26
add a comment |
You can't subtract one string from another string.
Instead of doing:
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
Do:
data: data.sort((a, b) => {
const asc = this.state.directionSymbol[key] === 'asc';
if (a[key] < b[key]) {
return asc ? -1 : 1;
} else if (a[key] > b[key]) {
return asc ? 1 : -1;
} else {
return 0;
}
)),
You can't subtract one string from another string.
Instead of doing:
data: data.sort((a, b) => (
this.state.directionSymbol[key] === 'asc'
? a[key] - b[key]
: b[key] - a[key]
)),
Do:
data: data.sort((a, b) => {
const asc = this.state.directionSymbol[key] === 'asc';
if (a[key] < b[key]) {
return asc ? -1 : 1;
} else if (a[key] > b[key]) {
return asc ? 1 : -1;
} else {
return 0;
}
)),
answered Nov 16 '18 at 21:15
Doug CoburnDoug Coburn
1,2911116
1,2911116
Thanks Doug, it was helpfull.
– Ivana
Nov 16 '18 at 21:26
add a comment |
Thanks Doug, it was helpfull.
– Ivana
Nov 16 '18 at 21:26
Thanks Doug, it was helpfull.
– Ivana
Nov 16 '18 at 21:26
Thanks Doug, it was helpfull.
– Ivana
Nov 16 '18 at 21:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345196%2fsorting-react-table-by-clicking-table-headers-for-columns-with-numerical-and-str%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown