Custom sorting (non-alphabetical)
The levels should be specified explicitly:
A$animal <- factor(A$animal, levels = c("dog", "elephant","cat"))
A$color <- factor(A$color, levels = c("green", "blue", "red"))
Then you order by the 2 columns simultaneously:
A[order(A$animal,A$color),]
# animal color
# 6 dog green
# 4 dog blue
# 5 dog red
# 9 elephant green
# 7 elephant blue
# 8 elephant red
# 3 cat green
# 1 cat blue
# 2 cat red
How to custom sort in JavaScript? Not alphabetical or numerical
A solution for more than only the listed words, with precedence of the listed words.
var data = [ 'Salisbury', 'Charlotte', 'Frankfurt', 'Düsseldorf', 'Raleigh', 'Cary', 'Wilson', 'High Point', 'Wake', 'New Jersey' ], customSort = [ 'Salisbury', 'High Point', 'Wake', 'Charlotte', 'Raleigh', 'Cary', 'Wilson' ], customLookup = customSort.reduce(function (r, a, i) { r[a] = ('000'+i).slice(-4); return r; }, {}), customSortFn = function (a, b) { return (customLookup[a] || a).localeCompare(customLookup[b] || b); };
document.write('<pre>' + JSON.stringify(data.sort(customSortFn), 0, 4) + '</pre>');
Sorting rows into non-alphabetical customized order
We can do this in base R
i1 <- with(df, ave(seq_len(nrow(df)), as.integer(gl(nrow(df), 3,
nrow(df))), FUN = function(i) c(i[c(2, 1, 3)])))
out <- df[i1,]
row.names(out) <- NULL
out
# Name Code
#1 Gas 2
#2 Tax 1
#3 Gas 2
#4 Gas 2
#5 Tax 1
#6 Gas 2
#7 Lunch 2
#8 Tax 1
#9 Lunch 2
#10 Car 2
#11 Tax 1
#12 Car 2
Or with tidyverse
library(tidyverse)
df %>% # initial dataset
uncount(Code, .remove = FALSE) %>%
mutate(rn = row_number()) %>%
group_by(grp = gl(n(), 3, n())) %>%
slice(c(2, 1, 3)) %>%
ungroup %>%
select(-rn, -grp)
Custom sort of a string array non-alphabetical
I would make an array of the letters of the alphabet, in the order that they should be sorted. Let's call this array sort
. You could then do something like
for (int i = 0; i < s1.Length; i++)
{
int s1Value = sort.IndexOf(s1[i]);
int s2Value = sort.IndexOf(s2[i]);
if(s1Value > s2Value)
return 1;
if(s2Value > s1Value)
return -1;
}
return 0;
Now a few disclaimers.
- I might have my -1 and 1 mixed up. I haven't looked at ICompare in a while and I forget which is which.
- This does not handle the two strings being different lengths.
- I have not tested this code.
You could improve on this by making a custom class to assign a weight to each character.
class CustomAlphabetSort
{
public char Character { get; set; }
public int Weight { get; set; }
}
From here you can sum of the value for each letter of a string and do the comparison that way. It might be a little more complex than your situation requires
Sort an array of object by a property (with custom order, not alphabetically)
You could take an object for the wanted order.
var array = [{ code: "RED", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }, { code: "RED", value: 0 }, { code: "GREEN", value: 0 }, { code: "BLUE", value: 0 }],
order = { GREEN: 1, BLUE: 2, RED: 3 };
array.sort(function (a, b) {
return order[a.code] - order[b.code];
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
How to sort a data frame by user-defined (e.g. non-alphabetic order)
You need to specify the levels
in factor
and then use order
with indexing:
zz <- "chrom start
chr2 39482
chr1 203918
chr1 198282
chrX 7839028
chr17 3874"
Data <- read.table(text=zz, header = TRUE)
library(Hmisc)
library(gdata)
Data$chrom <- reorder.factor(Data$chrom , levels = c("chr1","chr2", "chr17", "chrX"))
Data[order(Data$chrom), ]
chrom start
2 chr1 203918
3 chr1 198282
1 chr2 39482
5 chr17 3874
4 chrX 7839028
or you can use this:
> Data$chrom <- factor(chrom , levels = c("chr1","chr2", "chr17", "chrX"))
> Data[order(Data$chrom), ]
chrom start
2 chr1 203918
3 chr1 198282
1 chr2 39482
5 chr17 3874
4 chrX 7839028
or use this:
> Data$chrom <- reorder(Data$chrom, new.order=c("chr1","chr2", "chr17", "chrX"))
> Data[order(Data$chrom), ]
Related Topics
Convert Data.Frame Column Format from Character to Factor
Subset Data Frame Based on Multiple Conditions
How to Save Plots That Are Made in a Shiny App
R Conditional Evaluation When Using the Pipe Operator %≫%
Read All Files in a Folder and Apply a Function to Each Data Frame
How to Change the Default Library Path For R Packages
Wrap Long Axis Labels Via Labeller=Label_Wrap in Ggplot2
How to Extract Plot Axes' Ranges For a Ggplot2 Object
R on Macos Error: Vector Memory Exhausted (Limit Reached)
Read All Files in Directory and Apply Multiple Functions to Each Data Frame
Offline Install of R Package and Dependencies
How to Open CSV File in R When R Says "No Such File or Directory"
How to Calculate Mean/Median Per Group in a Dataframe in R
Understanding the Order() Function
Select Groups With More Than One Distinct Value