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

**URL:** <https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436>\
**Category:** Coding Questions\
**Created:** [December 9, 2018, 4:27am UTC](https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436 "2018-12-09T04:27:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![epicwyvern](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@epicwyvern](https://discourse.processing.org/u/epicwyvern)\
**Post date:** [December 9, 2018, 4:27am UTC](https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436/1 "2018-12-09T04:27:12Z")

</div>

hello guys

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

```auto
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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 9, 2018, 5:25am UTC](https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436/2 "2018-12-09T05:25:01Z")

</div>

just a first idea,  
load the file as a table and try  
[https://processing.org/reference/Table\_sort\_.html](https://processing.org/reference/Table_sort_.html)

```auto
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

```auto
// 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

---

<div class="post-metadata">

**Author:** ![epicwyvern](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@epicwyvern](https://discourse.processing.org/u/epicwyvern)\
**Post date:** [December 9, 2018, 5:27pm UTC](https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436/3 "2018-12-09T17:27:55Z")

</div>

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:

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

```

```auto
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:

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

```

thanks for your time 🙂

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

```auto
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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![epicwyvern](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@epicwyvern](https://discourse.processing.org/u/epicwyvern)\
**Post date:** [December 9, 2018, 11:48pm UTC](https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436/4 "2018-12-09T23:48:11Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 10, 2018, 3:49am UTC](https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436/5 "2018-12-10T03:49:28Z")

</div>

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-](http://processing.github.io/processing-javadocs/core/processing/data/Table.html#setColumnType-int-int-)

![snap-034](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2659116c04e9062fa049a6db5b5a7644543a6001.png)

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**
>
> ```auto
> // 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
>  
> */
> 
> ```

---

<div class="post-metadata">

**Author:** ![epicwyvern](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@epicwyvern](https://discourse.processing.org/u/epicwyvern)\
**Post date:** [December 10, 2018, 7:47am UTC](https://discourse.processing.org/t/processing-3-0-how-to-sort-string-array-by-value-of-substrings/6436/6 "2018-12-10T07:47:53Z")

</div>

ty very much!. you can close this thread now
