0
$\begingroup$

Following an answer provided by kglr in a previous post:

Hyperlink["https://mathematica.stackexchange.com/questions/189663/how-to-set-up-sortby-columns-in-dataset-dynamically"]

I have a dataset drawn from Import of a webpage of specimens in a museum collection (first 20 records used for this example) for which I want to sort on several columns of the dataset.

ClearAll;

ds = Dataset@{{<|"Catalog Number" -> "UBC 560305 ", 
  "Species" -> " Helicolenus percoides ", "Country" -> "New Zealand", 
  "Locality" -> " COOK STRAIT "|>}, {<|"Catalog Number" -> "UBC 730020 ", 
  "Species" -> " Sebastes aleutianus ", "Country" -> "", 
  "Locality" -> " AK SPRUCE ID "|>}, {<|"Catalog Number" -> "UBC 630676 ",
   "Species" -> " Sebastes aleutianus ", "Country" -> "", 
  "Locality" -> " USSR BERING SEA "|>}, {<|"Catalog Number" -> 
   "UBC 630677 ", "Species" -> " Sebastes aleutianus ", "Country" -> "", 
  "Locality" -> " USSR BERING SEA "|>}, {<|"Catalog Number" -> 
   "UBC 630684 ", "Species" -> " Sebastes aleutianus ", "Country" -> "", 
  "Locality" -> " USSR BERING SEA "|>}, {<|"Catalog Number" -> 
   "UBC 650054 ", "Species" -> " Sebastes aleutianus ", 
  "Country" -> "Canada", 
  "Locality" -> " SMITH SOUND MARGARET BAY "|>}, {<|"Catalog Number" -> 
   "UBC 700034 ", "Species" -> " Sebastes aleutianus ", 
  "Country" -> "Canada", 
  "Locality" -> 
   " SW VANCOUVER ID LA PEROUSE BANK "|>}, {<|"Catalog Number" -> 
   "UBC 720105 ", "Species" -> " Sebastes aleutianus ", 
  "Country" -> "United States (c", 
  "Locality" -> " AK UNIMAK ID "|>}, {<|"Catalog Number" -> "UBC 730024 ",
   "Species" -> " Sebastes aleutianus ", "Country" -> "United States (c", 
  "Locality" -> " AK WOODY ID "|>}, {<|"Catalog Number" -> "UBC 730027 ", 
  "Species" -> " Sebastes aleutianus ", "Country" -> "United States (c", 
  "Locality" -> " AK WOODY ID "|>}, {<|"Catalog Number" -> "UBC 650297 ", 
  "Species" -> " Sebastes alutus ", "Country" -> "Canada", 
  "Locality" -> " QUEEN CHARLOTTE ID "|>}, {<|"Catalog Number" -> 
   "UBC 760001 ", "Species" -> " Sebastes alutus ", "Country" -> "Canada",
   "Locality" -> 
   " QUEEN CHARLOTTE SD G B REED CRUISE "|>}, {<|"Catalog Number" -> 
   "UBC 650071 ", "Species" -> " Sebastes alutus ", 
  "Country" -> "United States (c", 
  "Locality" -> 
   " 26 MINUTES SE OF SIMEONOF ID "|>}, {<|"Catalog Number" -> 
   "UBC 650096 ", "Species" -> " Sebastes alutus ", 
  "Country" -> "United States (c", 
  "Locality" -> " AK NEAR TIGALDA ID "|>}, {<|"Catalog Number" -> 
   "UBC 650067 ", "Species" -> " Sebastes alutus ", 
  "Country" -> "United States (c", 
  "Locality" -> " AK S OF SEMIDI ID "|>}, {<|"Catalog Number" -> 
   "UBC 590669 ", "Species" -> " Sebastes auriculatus ", 
  "Country" -> "Canada", 
  "Locality" -> 
   " HECATE STRAIT BETWEEN CAPE BALL + LONG POINT "|>}, {<|"Catalog Number" -> "UBC 670012 ", "Species" -> " Sebastes aurora ", 
  "Country" -> "Canada", 
  "Locality" -> 
   " LA PEROUSE BANK SW VANCOUVER ID "|>}, {<|"Catalog Number" -> 
   "UBC 660134 ", "Species" -> " Sebastes babcocki ", 
  "Country" -> "Canada", 
  "Locality" -> " B.C. "|>}, {<|"Catalog Number" -> "UBC 760019 ", 
  "Species" -> " Sebastes baramenuke ", "Country" -> "Japan", 
  "Locality" -> " NEAR HACHINOHE AOMORI PREF "|>}, {<|"Catalog Number" -> 
   "UBC 560344 ", "Species" -> " Sebastes baramenuke ", 
  "Country" -> "Japan", "Locality" -> " NO LOCALITY DATA "|>}};

In this MSE question (189663) kgrl provides the following code in the example, as follows:

(*ClearAll[fDatasetSortBy]
fDatasetSortBy[s1_,s2_,s3_]:=ds[SortBy[{#[s1]&,#[s2]&,#[s3]&}]]*)

(*fDatasetSortBy["C_2","C_3","C_1"]*)

that I have modified for my dataset, which contains an additional column and different column labels. kglr's answer works when I run his example on my system using Mathematica 13.1.0.0.

ClearAll[fDatasetSortBy]
fDatasetSortBy[s1_, s2_, s3_, s4_] := 
ds[SortBy[{#[s1] &, #[s2] &, #[s3] &, #[s4] &}]]

However, when I run this using the following substitutions, it fails to sort as intended.

fDatasetSortBy["Country", "Species", "Catalog Number", "Locality"]

as does:

fDatasetSortBy["Species", "Catalog Number", "Country", "Locality"]

Can anyone explain why my modifications fail and how to successfully sort on multiple columns of a dataset?

$\endgroup$

1 Answer 1

2
$\begingroup$

Instead of ds = Dataset@{{<|"Catalog Number" -> ...

use: ds = Dataset@Flatten@{{<|"Catalog Number" -> "UBC ...

Then it works as expected.

enter image description here

$\endgroup$
1
  • $\begingroup$ The double curly braces make all the difference in the world. Thanks for catching my oversight. $\endgroup$ Commented Nov 21, 2022 at 16:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.