(Processing 3.0) how to sort string array by value of substrings?

hello guys

im in a bit of a dilemma. i am trying to sort the following text file:

a         a        1
aasd      asdasa   23
aghf      bsad     12
ytyi      qwea     1222313
hlkjojto  erewrui  1222
aerwjdu   sdahdsu  123422313

now, i want to sort the following text file of strings, which is loaded in processing 3 via:

String [] bookmarks;

..................

bookmarks = loadStrings("bookmark.txt"); 

i want to sort the strings only based on the last numbers in each line, so that the result becomes the following:

a         a        1
aghf      bsad     12
aasd      asdasa   23
hlkjojto  erewrui  1222
ytyi      qwea     1222313 
aerwjdu   sdahdsu  123422313

will someone please help me with this?

1 Like

just a first idea,
load the file as a table and try
https://processing.org/reference/Table_sort_.html

table.sort(2);

but only if the 3 column structure is FIX

sorry, more restrictions: for table it must be “Tab” or “,” separated, " " will not work

// https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436
// https://processing.org/reference/loadTable_.html
// https://processing.org/reference/Table_sort_.html
// https://processing.org/tutorials/data/

String infilename = "data/bookmarks.txt";
String outfilename = "data/bookmarks_sort.txt";
Table mytable;

void setup() {
  mytable = loadTable(infilename,"tsv");
  // this "tsv" works only for <Tab> separated values
  // but  "csv" would work for "," separated values
  // if the file has a first header line must use "header, tsv" so it would not be sorted as data
  // a custom split like " " i not know how to do using table, only by loadString split
  println(mytable.getRowCount() + " total rows in table");
  println(mytable.getColumnCount() + " total cols in table");
  mytable.sort(2);
  saveTable(mytable,outfilename,"tsv");
  exit();
}


/*
see file
data/bookmarks_sort.txt
content:
a  a  1
aghf  bsad  12
hlkjojto  erewrui  1222
ytyi  qwea  1222313
aerwjdu  sdahdsu  123422313
aasd  asdasa  23
*/

pls. see this last line v.s. your sorted 3d. line !!!

note:
tested on win 7 / 32bit / processing 3.4

Hello, your solution works well. i changed the gaps in the text file from random spaces to the commas you mentioned to follow the csv format:

a     a     1
aghf    bsad     12
aasd    asdasa     23
hlkjojto     erewrui     1222
ytyi     qwea     1222313 
aerwjdu     sdahdsu     123422313
a,a,1
aasd,asdasa,23
aghf,bsad,12
ytyi,qwea,1222313
hlkjojto,erewrui,1222
aerwjdu,sdahdsu,123422313

this is then the output of bookmarks_sort.txt:

a,a,1
aghf,bsad,12
hlkjojto,erewrui,1222
ytyi,qwea,1222313
aerwjdu,sdahdsu,123422313
aasd,asdasa,23

thanks for your time :slight_smile:

however, i want to know if its possible to replace the inbuilt sort you used here:

mytable.sort(2);

i want to replace the inbuilt sort with a custom bubble and insertion sort, so that it sorts the third row based on numerical value rather than only the first digit. basically what i am saying is that instead of sorting the file like this:

a,a,1
aghf,bsad,12
hlkjojto,erewrui,1222
ytyi,qwea,1222313
aerwjdu,sdahdsu,123422313
aasd,asdasa,23

i want to sort the last numbers based on how big they are, like this:

a,a,1
aghf,bsad,12
aasd,asdasa,23
hlkjojto,erewrui,1222 
ytyi,qwea,1222313 
aerwjdu,sdahdsu,123422313 

it would be fantastic if you show me how to do it via bubble and/or insertion sort.

is anyone still around to help?

ive managed to sort the first two rows which consist of names of books and authors, but i am still having trouble sorting the numerical values of the last row.

i have tried:

mytable.sort(int(2));

but it still doesnt work.

i wold like if someone could show me how to sort the last row of numbers via bubble and insertion sort. if thats not possible than a way to do it with inbuilt sort would be fine.

ok, sorry, yes there is a need to first set the column to NUMBER instead TEXT
http://processing.github.io/processing-javadocs/core/processing/data/Table.html#setColumnType-int-int-

snap-034

i told you the “table” way is just a first idea.
but in this time would easily made your own loadstring // sort array // version
( and possibly would have learned more about loops …)

but usually a csv file has a header ( column name )

example v0.3
// https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436
// https://processing.org/reference/loadTable_.html
// https://processing.org/reference/Table_sort_.html
// https://processing.org/tutorials/data/
// http://processing.github.io/processing-javadocs/core/processing/data/Table.html#setColumnType-int-int-
// v 0.2 change from tsv to csv
// v 0.2 use col 2 as INT so get a number sort instead a lexical sort
// v 0.3 make header

String infilename  = "data/bookmarks.txt";
String outfilename = "data/bookmarks_sort.txt";
Table mytable;
int tcols, trows, numcol = 2;
boolean dprint = true;


void getf_tabledata() {                             // get tabledata from file
  mytable = loadTable(infilename, "header, csv");
  tcols = mytable.getColumnCount();
  if (dprint) println("total cols in table: "+tcols);
  trows = mytable.getRowCount();
  if (dprint) println("total rows in table: "+trows);
  if (dprint) println("header: ");
  for ( int i =0; i < tcols; i++) {
    if (dprint) println("headerline col: "+i+" "+ mytable.getColumnTitle(i));
  }
}

void sort_tabledata() {                             // sort numerically a specific column
  mytable.setColumnType(numcol, Table.INT); 
  mytable.sort(numcol);  
  if (dprint) println("list all rows column "+numcol+" sorted by number");
  for ( int i = 0; i < mytable.getRowCount(); i++ ) {
    TableRow thisrow = mytable.getRow(i);
    if (dprint) println("row "+i+" "+thisrow.getInt(numcol));
  }
}

void save_tabledata() {                             // save to other file
  saveTable(mytable, outfilename, "csv");
}

void setup() {
  getf_tabledata();
  sort_tabledata();
  save_tabledata();
  exit();
}

/*
data/bookmarks.txt
 content:
 
 col a,col b,col c
 a,aaaa,1
 b,asda,23
 c,bsad,12
 d,qwea,1222313
 e,erew,1222
 f,sdah,123422313
 g,qwer,17
 
 */

ty very much!. you can close this thread now