Sorting contents of a Data Frame in Julia - GeeksforGeeks

25 Mar, 2021
Sorting is a technique of storing data in sorted order. Sorting can be performed using sorting algorithms or sorting functions. to sort in a particular dataframe then extracting its values and applying the sorting function would easily sort the contents of a particular dataframe. Julia provides various methods that can be used to perform the sorting of DataFrames. 
Remember to add the following package before starting i.e DataFrames with help of below code:
using Pkg
Pkg.add("DataFrames")
Julia provides some methods for sorting data of a DataFrame:
sort() function: This function returns a sorted array or sorted copy of an array
sort() function passing an algorithm: This function returns a sorted array with the help of an applied algorithm
sortperm() function: This function returns a list of indices that one can use on the collection to produce a sorted collection.
partialsortperm() function: This function partially sorts the algorithm up to a particular rang or permutation.
sort() function with rev=True: This function will sort the content of the dataframe into descending order.
sort!(): This function passing the dimension, this function can sort multidimensional arrays of DataFrames.
partialsortperm(): This function returns a partial permutation DataFrame’s column of the vector
sort() Function in Julia is the most basic sorting method that can be used to sort data of a dataframe. 
Approach:
First, you can create the dataframe
The sort() function has arguments like the vector and the order in which the columns need to be sorted.
Julia
# Creating a dataframedf1 = DataFrame(b = [:Hi, :Med, :Hi, :Low, :Hi],               x = ["A", "E", "I", "O","A"],               y = [6, 3, 7, 2, 1],               z = [2, 1, 1, 2, 2]) # Method1sort(df1,[:z,:y]) # sorted z then y
  
 
Julia allows passing the algorithm type to sort() function to sort the column. sort(dataframe.columnheader; alg=QuickSort) function takes column name and algorithm type as an argument.
 
Approach:
 
Here, the sort() function is applied to a specific column.
It is passed as an argument in the sort function
Then the algorithm with which you want to sort the particular column is also passed as an argument
Store the returned value of this function in a separate variable
Then update in the particular column
 
Julia
# Method2 Algorithm(Quicksort)# Sorting a particular column and storing it in ss = sort(df1.y; alg = QuickSort) # Now giving the value of s to the dataframe's y headerdf1.y = sdf1 # printing the sorted y
  
 
sort(dataframe.columnheader; alg=PartialQuickSort(range)) function is passed with PartialQuickSort algorithm to sort the column upto a certain limit which is passed in the algorithm.
 
Approach:
 
Here, the sort() function is applied to a specific column.
It is passed as an argument in the sort function
Then the algorithm(PartialQuickSort) with which you want  to sort the particular column is also passed as an argument
Store the returned value of this function in a separate variable
Then update in the particular column
 
Julia
# Method3 Algorithm(PartialQuickSort)# If we want sort a column upto a certain numberB = 3t = sort(df1.z; alg = PartialQuickSort(B)) # passing the t variable in the dataframedf1.z = tdf1
  
 
sortperm() function is passed with the column name, to sort the column and return indexes of the sorted column.
 
Approach:
 
First store the particular column in which you want to apply this sorting in a separate variable
Apply the sortperm() function and pass the variable as argument this will return the sorted indexes of the particular column and store the returned indexes in a separate variable
Then traverse using the for loop in the variable where the indexes are stored
Print using the for loop and pass the index in the variable where the particular column was stored.
 
Julia
# Method4r = df1.y # returned indexes of the elementsk = sortperm(r) # traversing through indicesfor i in k    println(r[i]) end
sort(dataframe.column;alg=InsertionSort) function is passed with InsertionSort algorithm to sort the column up to certain limit which is passed in the algorithm.
Approach:
Creating a new dataframe  and applying the sort() function
This time the algorithm used is insertion sort
Then the algorithm(InsertionSort) with which you want  to sort the particular column is also passed as an argument
Store the returned value of this function in a separate variable
Then update in the particular column
Julia
# Created new dataframe as df2df2 = DataFrame(x = [11,12, 13, 10, 23],                y = [6, 3, 7, 2, 1],                z = [2, 1, 1, 2, 2])# Method5s2 = sort(df2.x; alg = InsertionSort) # now update the df2.xdf2.x = s2df2
  
 
Method 6: Use of partialsortperm() function
 
partialsortperm(dataframe.column, range) function is an advanced form of sortperm() function which will return the indexes of the values which are in range. This partially sorts the column.
 
Approach:
 
Storing the particular column which needs to be sorted in another variable
Applying the partialsortperm() function passing the vector and the range till which it needs to be sorted
Finally, we can update with the help of passing the result into the particular DataFrame’s column
Now printing the dataframe would simply print with updated value
 
Julia
# Method6a = df2.ya = a[partialsortperm(a, 1:5)]a
sort(dataframe,rev=True) function is passed with dataframe and rev variable to sort the column. This function basically reverses or gives a descending order of the column passed.
Approach:
Sorting the dataframe’s particular column in the descending order using the sort() function
First storing the particular column in the variable
And applying the sort() function and passing the reverse of the particular column as rev = true
This now will sort in the descending order
At last, updating the dataframe by passing the variable into the dataframe
Julia
# Method7s2 = sort(df2, rev=true)df2 = s2 #updating the whole dataframedf2
  
 
      
 
 
sort!(vector,dim) function is passed with dataframe and dimension in which we want to sort the column (dimension means dim=1 means row and dim=2 means column).
 
Approach:
 
The function for now applying the sort with user’s choice to either sort by row or column
Sorting by row is done by passing  vector into the sort!() function
Also, we need to pass the dim=1 which means to traverse row-wise
This function will print the sorting in the row manner
Now applying  the same function by just passing dim=2 to sort in column manner.
This now would print the sorted vector in the column manner.
 
Julia
# Method8B = [4 3; 1 2]sort!(B, dims = 1); B # sorting through rowsort!(B, dims = 2); B # sorting through column
  
 
arorakashish0911
julia-DataFrames
Julia
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Decision Making in Julia (if, if-else, Nested-if, if-elseif-else ladder)
Get array dimensions and size of a dimension in Julia - size() Method
Exception handling in Julia
Searching in Array for a given element in Julia
Find maximum element along with its index in Julia - findmax() Method
Get number of elements of array in Julia - length() Method
Join an array of strings into a single string in Julia - join() Method
Working with Excel Files in Julia
File Handling in Julia
Getting last element of an array in Julia - last() Method