Sorting in React Select - reactjs

I'd like to know if it is possible to set the sorting by alphabetical and number order. Here is my code, and it only does the alphabetical sorting but not the numeric one.
resellerList.sort((a1, a2) => {
const b1 = a1.resellerName.toLowerCase();
const b2 = a2.resellerName.toLowerCase();
if (b1 == b2) return 0;
if (b1 > b2) return 1;
return -1;
})

Here is a solution if the names always starts with 'client' following a number as in your example.
// input = 'client 6' then output = 6
const sliceAndParseInt = client => {
const client = client.resellerName.toLowerCase()
return parseInt(client.slice(6))
}
const sorted = resellerList.sort((a, b) => {
if (sliceAndParseInt(a) > sliceAndParseInt(b)) return 1
if (sliceAndParseInt(a) < sliceAndParseInt(b)) return -1
return 0
})
Otherwise, names are not always following the same convention unlike your example, you should change all the numbers to an equal length, e.g. 1 to 01;
'14' > '2' # return false
'14' > '02' # return true
Regards:)

Related

Sort an array of date ranges in typeScript

Array(6) [
"10-2022 - 12-2022",
"08-2022 - 09-2022",
"07-2023 - 10-2023",
"04-2022 - 07-2022",
"01-2023 - 06-2023",
"01-2022 - 03-2022" ]
I want to sort this array of date ranges to show the latest date range at the beginning of the array. Having some trouble because the dates are in string format.
Try with this utility function:
const arr = [
"10-2022 - 12-2022",
"08-2022 - 09-2022",
"07-2023 - 10-2023",
"07-2023 - 11-2023",
"04-2022 - 07-2022",
"01-2023 - 06-2023",
"01-2022 - 03-2022"
];
const getYearMonth = (date) => {
const dateSplit = date.split('-');
if (dateSplit.length < 2) return '';
return dateSplit[1] + '-' + dateSplit[0];
}
const sortedArr = arr.sort((a, b) => {
const aSplit = a.split(' - ');
const bSplit = b.split(' - ');
const aYearMonthStart = getYearMonth(aSplit[0]);
const bYearMonthStart = getYearMonth(bSplit[0]);
// Sort decreasing by start
if (aYearMonthStart > bYearMonthStart) return -1;
if (aYearMonthStart < bYearMonthStart) return 1;
// Sort decreasing by end date if start date equal
const aYearMonthEnd = getYearMonth(aSplit[1]);
const bYearMonthEnd = getYearMonth(bSplit[1]);
if (aYearMonthEnd > bYearMonthEnd) return -1;
if (aYearMonthEnd < bYearMonthEnd) return 1;
// Equal dates
return 0;
})
console.log(sortedArr);
Array.sort((a, b) => b.localeCompare(a))

React table sort and search

i have data like
const data =[
{
name:'gacvuc',
pot1[
{
density:1
}
]
pot2[
{
density:2
}
]
}
.....
]
now i made a toggle in that do we wanna see pot1 density or pot2 density
and made a table with that like this
<tablebody>
data.map(a,in)=>{
const show= toggleChoice === 0 ? a.pot1 : a.pot2;
}
return(
<tablecell>{a.name}</tablecell>
<tablecell>{show.density}</tablecell>
</tablebody>
);
now what i wanna do is i wanna sort by density in ascending or descending or search by it's name can anyone show me how to do that
First, I would recommend taking a look at the format of your data and looking for mistakes.
To sort your data, you can simply use the built-in sort method and pass in a function that gives instructions on how to sort two different objects in your data.
data.sort(function(a, b) {
// you can define this based on your toggle variable
const pot = "pot1";
// densities of both object
const aDensity = a[pot][0].density;
const bDensity = b[pot][0].density;
// compare them and then return -1, 0, or 1 accordingly
if (aDensity == bDensity) { // if they are equal, return 0
return 0;
}
else if(aDensity > bDensity) { // > return 1
return 1;
}
else { // < return -1
return -1;
}
})
In this snippet, you get the densities of two arbitrary objects, a and b, and then return -1, 0, or 1 depending on the values of the density. In this example. I sorted them in ascending order, but if you want descending, you could swap the 1 for -1 and vice versa in the code.

Why does the Object.Keys.().length is showing non-empty value even it has 0 items in mongoDB?

This is the command prompt output error for Objects key length.
Here Object.keys(data.Addtasks).length = 34
Addtasks array length is greater than 0. I dont know why this is showing non-zero value even though in database it has 0 objects list.
Below is the MongoDB schema pic:
And this is my expressjs code:
router.route("/settingsClients").get(function(req, res) {
User.findOne({_id:req.user.id}, function(err, data) {
console.log("Object.keys(data.Addtasks).length =" + Object.keys(data.Addtasks).length );
if (Object.keys(data.Addtasks).length > 0) {
console.log("Addtasks length is greater than 0");
let wordsCount = 0;
data.Addtasks.forEach(element => {
wordsCount = wordsCount + element.words;
});
let subscriptionTCR = req.user.subscriptionTotalCount - wordsCount;
res.render('settingsClient',{subscriptionTotalCountRemaining: subscriptionTCR});
}
else {
res.render('settingsClient',{subscriptionTotalCountRemaining: req.user.subscriptionTotalCount});
}
});
})

How to sort an array of objects by calculated value

Hello I'm trying to sort an array of objects by value, trought a function that calculates the average of the values in that objects property, which is an array of integers, then sorts it in decreasing order.
// SORT BY SCORE BY MOST SCORED
function sortByScoreDown() {
arrayLivros.sort((a, b) => fullscoreForSort(b._scores) - fullscoreForSort(a._scores));
}
// CALCULATE FULLSCORE
function fullscoreForSort(givenScores) {
let score = 0;
let total = givenScores.length - 1; // -1 BECAUSE BOOK._SCORES STARTS WITH AN ARRAY WITH 0 AS FIRST VALUE FOR SIMPLIFICATION
if (total != 0) {
let summedScore = givenScores.reduce((sum, add) => sum + add);
let score = summedScore / total;
}
return score;
}
// VALUES
newBook1 = {_title: book1,
_scores: [0,100,50]}
newBook2 = {_title: book2,
_scores: [0,100,100]}
newBook3 = {_title: book3,
_scores: [0,50,50]}
newBook4 = {_title: book3,
_scores: [0,30,30]}
arrayBooks = [newBook1, newBook2, newBook3, newBook4];
// EXCPECTED RETURN
arrayBooks = [newBook2, newBook1, newBook3, newBook4];
Thanks in advance.

how to sort array by date and count in ionic 2

I want to sort my array by date and count, i'm able to sort array only by date my data is as below
count date
"0" "2018-03-06T07:09:02+00:00"
"0" "2018-03-06T07:07:02+00:00"
"0" "2018-03-06T07:06:03+00:00"
"0" "2018-03-06T07:02:06+00:00"
"0" "2018-03-06T06:39:55+00:00"
"0" "2018-03-06T06:30:14+00:00"
"1" "2018-03-06T06:22:20+00:00"
"1" "2018-03-06T06:07:04+00:00"
"0" "2018-03-06T06:03:17+00:00"
"14" "2018-03-01T10:28:27.998000+00:00"
"0" null
"0" null
"0" null
my code is below..
this.nodelist.sort((a, b) => {//lastDate dsc
if (new Date(b.lastDate) > new Date(a.lastDate)) {
return 1;
}
if (new Date(b.lastDate) < new Date(a.lastDate)) {
return -1;
}
return 0;
});
i want to sort array by count and date both means if array is having count > 0 then it should be first then count with zero and on last all other records. can anyone help me to solve this?
You can use your code and modify it like this:
this.nodelist.sort((a, b) => {
// 1st property, sort by count
if (a.count > b.count)
return -1;
if (a.count < b.count)
return 1;
// 2nd property, sort by date
if (new Date(b.lastDate) > new Date(a.lastDate))
return 1;
if (new Date(b.lastDate) < new Date(a.lastDate))
return -1;
return 0;
});
How does it work?
The first two if statements will sort the array by count.
If count is equal, the code will consider the second property (lastDate).
Try this:
let xyz = numbers.sort(function(a, b) {
var countA = a.count;
var countB = b.count;
var dateA = new Date(a.date);
var dateB = new Date(b.date);
if(countA == countB)
{
return (dateB < dateA) ? -1 : (dateB > dateA) ? 1 : 0;
}
else
{
return (countB < countA) ? -1 : 1;
}
});
console.log(xyz);

Resources