Skip to content

Latest commit

 

History

History
77 lines (60 loc) · 1.79 KB

sortusage.md

File metadata and controls

77 lines (60 loc) · 1.79 KB

Here are 10 advanced examples of using the sort function in Julia:

  1. Sorting an array of integers in ascending order:
arr = [5, 2, 7, 1, 3]
sorted_arr = sort(arr)
  1. Sorting an array of strings in descending order:
arr = ["apple", "banana", "cherry", "date"]
sorted_arr = sort(arr, rev=true)
  1. Sorting a 2D array by a specific column:
arr = [[5, 2], [3, 1], [7, 4], [1, 3]]
sorted_arr = sort(arr, by=x->x[2])
  1. Sorting a custom type based on a specific field:
struct Person
    name::String
    age::Int
end

people = [Person("Alice", 25), Person("Bob", 30), Person("Charlie", 20)]
sorted_people = sort(people, by=x->x.age)
  1. Sorting a dictionary by its keys:
dict = Dict("b" => 2, "a" => 1, "c" => 3)
sorted_dict = sort(collect(dict))
  1. Sorting a dictionary by its values:
dict = Dict("b" => 2, "a" => 1, "c" => 3)
sorted_dict = sort(collect(dict), by=x->x[2])
  1. Sorting a dataset by multiple columns:
using DataFrames

df = DataFrame(a=[1, 2, 1], b=[3, 2, 1], c=[5, 4, 3])
sorted_df = sort(df, [:a, :b], rev=[false, true])
  1. Sorting a dataset in-place:
using DataFrames

df = DataFrame(a=[2, 1, 3])
sort!(df, :a)
  1. Sorting a dataset using a custom comparison function:
using DataFrames

df = DataFrame(a=[2, 1, 3])
sorted_df = sort(df, lt=(x, y)->x.a > y.a)
  1. Sorting a dataset using a custom transformation of values:
using DataFrames

df = DataFrame(a=[2, 1, 3])
sorted_df = sort(df, by=x->-x.a)

These examples demonstrate various ways to use the sort function in Julia to sort arrays, dictionaries, datasets, and custom types. The sort function provides flexibility in sorting based on different criteria and allows for both ascending and descending order sorting.