# How to sort words alphabetically from a String?

**URL:** https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767
**Category:** Coding Questions
**Created:** [July 17, 2019, 9:47pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767 "2019-07-17T21:47:38Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 17, 2019, 9:47pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/1 "2019-07-17T21:47:38Z")

</div>

Hello,  
The code below compiles, but the way the words are displayed is really strange (last word on the left and then the other words on its right…) how come ?!!  
I want to sort the words in alphabetical order and change their coordinates according to mouseX  
A-words at the top, then B-words below and so on according to their index.  
Thanks a lot already for your help.  
Best,  
L

```auto
String wordsList="afaint,afar,all,and,away,folly,for,from,given,glimpse,is,need,over,see,seeing,seem,that,the,there,this,to,where,what,word,";
String message;
String []lines;
boolean drawText;
PFont font;
String [] words;

// *****************************************************************************************************************************************

void setup() {
  size(1080, 1600);

  lines = loadStrings("Beckett.txt"); //link to the text
  message= join(lines, " "); // join all the words together
  lines= split(message, ","); //split the text line by line
  words = splitTokens(message, " "); // now split each line into words?!
  font = createFont("helvetica", 30);
  textFont(font);
  textAlign(LEFT);
}

// *****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(500, 100);
  smooth();
  int posX = 0;
  int posY = 0;

  for (int j=0; j<words.length; j++) {
   
    int index = wordsList.indexOf(words[j]);
    //if (index<0) continue;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    //float sortX = 0;
    //float interX = lerp(posX, sortX, m);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    posX +=textWidth(words[j])+10;
    
    if (words[j].equals(",") || words[j].indexOf(",")>0) {
      // new line 
      posX=0; 
      posY += 40;
    }

    fill(255);
    text(words[j], posX, interY);
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 17, 2019, 10:43pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/2 "2019-07-17T22:43:48Z")

</div>

> [@lolonulu](#):
>
> I want to sort the words in alphabetical order…

`java.util.Arrays.sort(words);`

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object[])](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object%5B%5D))

> [@File Listing Order](https://discourse.processing.org/t/file-listing-order/7148/41):
>
> [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object[])](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(java.lang.Object%5B%5D)) static final String PICS\_EXTS = "extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp"; static final float FPS = .25; PImage[] images; void setup() { size(1200, 600); frameRate(FPS); imageMode(CENTER); final File dir = dataFile(""); println(dir); String[] imagePaths = {}; int imagesFound = 0; if (dir.isDirectory()) { imagePaths = listPaths(dir.getPath(), "files", "recursi…

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 18, 2019, 9:56am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/3 "2019-07-18T09:56:29Z")

</div>

Dear @GoToLoop,  
Thank you very much for your answer,  
but the problem stay the same!?  
I certainly don’t understand how it works…

```auto
String wordsList="folly, folly for to, for to, what is the word,";
String message;
String []lines={"folly,","folly for to,","for to,","what is the word,"};
boolean drawText;
PFont font;

// *****************************************************************************************************************************************

void setup() {
  size(1080, 1600);
  font = createFont("helvetica", 30);
  textFont(font);
  // lines = loadStrings("Beckett.txt"); //link to the text
  message= join(lines, " "); // join all the words together
  lines= split(message, ","); //split the text line by line
  java.util.Arrays.sort(lines);
}

// *****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(100, 100);
  smooth();
  int posX = 0;
  int posY = 0;

  String [] words = split(message, " ");
  for (int j=0; j<words.length; j++) {
    int index = wordsList.indexOf(words[j]);
    println(index);

    if (index<0)continue;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortX = 0;
    float interX = lerp(posX, sortX, m);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    posX +=textWidth(words[j])+10;

    if (words[j].equals(",")|| words[j].indexOf(",")>0) {
      // new line 
      posX=0; 
      posY += 50;
    }

    textAlign(LEFT, LEFT);
    textSize(30);
    fill(255);
    text(words[j], posX, interY);
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 18, 2019, 10:07am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/4 "2019-07-18T10:07:16Z")

</div>

> [@lolonulu](#):
>
> I certainly don’t understand how it works…

Just print the array’s state before & after **sort()**:

```auto
println("Before sorting:");
printArray(lines);

java.util.Arrays.sort(lines);

println("\nAfter sorting:");
printArray(lines);

```

Alternative, you can also try out Processing’s own **sort()** function:

> **[sort() / Reference](https://processing.org/reference/sort_.html)**
>
> Sorts an array of numbers from smallest to largest, or puts an array of words in alphabetical order. The original array is not modified; a re-ordered array is returned. The count parameter stat…

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 18, 2019, 10:28am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/5 "2019-07-18T10:28:22Z")

</div>

Thanks for your reply @GoToLoop,  
Okay I did understood, but still the order of the word is messy in x, how come?!  
a problem with textWidth()?!

```auto
String wordsList="folly,folly for to,for to,what is the word";
String message;
String []lines={"folly,","folly for to,","for to,","what is the word"};
boolean drawText;
PFont font;

// *****************************************************************************************************************************************

void setup() {
  size(1080, 1600);
  font = createFont("helvetica", 30);
  textFont(font);
  // lines = loadStrings("Beckett.txt"); //link to the text
  message= join(lines, " "); // join all the words together
  lines= split(message, ","); //split the text line by line
  println("Before sorting:");
  printArray(lines);
  java.util.Arrays.sort(lines);
  println("After sorting:");
  printArray(lines);
}

// *****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(100, 100);
  smooth();
  int posX = 0;
  int posY = 0;
  lines = split(message, " ");
  for (int i=0; i<lines.length; i++) {
    int index = wordsList.indexOf(lines[i]);
    if (index<0)continue;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortX = 0;
    float interX = lerp(posX, sortX, m);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    posX +=50;

    if (lines.equals(",")|| lines[i].indexOf(",")>0) {
      posX=0; 
      posY+= 50;
    }
   //textAlign(CENTER, CENTER);
    textSize(30);
    fill(255);
    text(lines[i], posX, interY);
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 18, 2019, 10:49am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/6 "2019-07-18T10:49:48Z")

</div>

> [@lolonulu](#):
>
> , but still the order of the _word_ is messy in _x_,

There aren’t any variables named exactly _word_ nor _x_ in your posted code!

Also, you’re reassigning field _lines_ w/ another String[] array inside **draw()**:  
`lines = split(message, " ");`

Obviously after that, the previously **sort()** String[] array from **setup()** is gone!

However, all of this is already beyond what you had specifically requested in this topic here.

I believe this is all being debated in your previous topic below:

> [@How to break line using loadspring()?](https://discourse.processing.org/t/how-to-break-line-using-loadspring/12668/):
>
> Hello, I’d like to split my text into Strings and to move the same Strings to new coordinates. First when I split the text I don’t manage to insert a line break, how should I proceed? Thanks a lot in advance for your help. Best, L PFont f; String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ2019,.;:!?\n "; String wordsList="FOLLY,WHAT,FOR,TO,IS,THE,WORD,ALL,GIVEN,IS,FROM,ALL,THAT,SEEING,THIS,SEE,GLIMPSE,NEED,SEEM,WHERE,OVER,AWAY,AFAR,AFAINT"; String []lines; String message; int posX = 20; int posY =…

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 10:52am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/7 "2019-07-18T10:52:24Z")

</div>

I failed to help him there that’s why he started a new thread

Maybe you can help him?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 18, 2019, 10:55am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/8 "2019-07-18T10:55:47Z")

</div>

> [@Chrisir](#):
>
> Maybe you can help him?

I still don’t get what exactly the OP wants the code to do. 😕

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 18, 2019, 11:28am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/9 "2019-07-18T11:28:14Z")

</div>

Dear @Chrisir and @GoToLoop,  
Thank you very much for helping me.  
I am so sorry if I cause you problems. In fact I want something very simple:

1. I’d like to sort the words from a string (text) THIS WORKS
2. I’d like those words to move from their original positions (posY) to a new position (interY)  
according to mouseX position THIS ALSO WORKS
3. But before the text get transformed it is not in the wright order: the last word of each sentence is on the left and the other words on the right side. I’d like the text to look like this before changing its coordinates:  
folly  
folly for to  
for to  
what is the word  
folly from this  
all this  
folly from all this

and so on…  
I hope this time I managed to explain yu clearly what I’d like to do?!  
Looking forward to reading you

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 18, 2019, 12:02pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/10 "2019-07-18T12:02:44Z")

</div>

Hey There!

What it seems to me is when you sort the array, the comma which are present within each String foil with the order of each String. What I would do is **add a comma at each position after the splitting**.

```auto
for(int i = 0 ; i < lines.length - 1 ; i++){
      lines[i] = lines[i]+",";
}

```

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 18, 2019, 12:44pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/11 "2019-07-18T12:44:51Z")

</div>

Hey @InferNova,  
Thanks a lot for your answer and suggestion.  
Do you mean like this ?!  
Thanks in advance.  
Best,  
L

```auto
 String wordsList="folly, folly for to, for to, what is the word";
String message;
String []lines={"folly", "folly for to", "for to", "what is the word"};
boolean drawText;
PFont font;

// *****************************************************************************************************************************************

void setup() {
  size(1080, 1600);
  font = createFont("helvetica", 30);
  textFont(font);
  // lines = loadStrings("Beckett.txt"); //link to the text
  message= join(lines, " "); // join all the words together
  //lines= split(message, ","); //split the text line by line
  lines = split(message, " "); //split the text word by word
  println("Before sorting:");
  printArray(lines);
  java.util.Arrays.sort(lines);
  println("After sorting:");
  printArray(lines);

  for (int i=0; i<lines.length-1; i++) {
    lines[i] = lines[i]+",";
  }
}

// *****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(100, 100);
  smooth();
  int posX = 0;
  int posY = 0;
  for (int i=0; i<lines.length; i++) {   
    int index = wordsList.indexOf(lines[i]);
    if (index<0)continue;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortX = 0;
    float interX = lerp(posX, sortX, m);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    posX +=textWidth(lines[i])+10;

    if (lines.equals(",")|| lines[i].indexOf(",")>0) {
      posX=0; 
      posY+= 30;
    }
    textSize(30);
    fill(255);
    text(lines[i], posX, interY);
  }
}

```

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 18, 2019, 12:59pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/12 "2019-07-18T12:59:42Z")

</div>

Do this before you do the split and join. I meant to say after sorting not splitting.

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 18, 2019, 1:15pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/13 "2019-07-18T13:15:40Z")

</div>

Thank you for your help  
okay, here before splitting and after sorting…  
Still same strange behavior!?

```auto
String wordsList="folly,folly for to,for to,what is the word";
String message;
String []lines={"folly,", "folly for to,", "for to,", "what is the word,"};
boolean drawText;
PFont font;

// *****************************************************************************************************************************************

void setup() {
  size(1080, 1600);
  font = createFont("helvetica", 30);
  textFont(font);

  println("Before sorting:");
  printArray(lines);
  java.util.Arrays.sort(lines);
  println("After sorting:");
  printArray(lines);

  for (int i = 0; i < lines.length-1; i++) {
    lines[i] = lines[i]+",";
  }
  // lines = loadStrings("Beckett.txt"); //link to the text
  message= join(lines, " "); // join all the words together
  lines= split(message, ","); 
  lines = split(message, " ");//split the text line by line
}

// *****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(100, 100);
  smooth();
  int posX = 0;
  int posY = 0;

  for (int i=0; i<lines.length; i++) {
    int index = wordsList.indexOf(lines[i]);
    if (index<0)continue;
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortX = 0;
    float interX = lerp(posX, sortX, m);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    posX +=50;

    if (lines.equals(",")|| lines[i].indexOf(",")>0) {
      posX=0; 
      posY+= 50;
    }
    //textAlign(CENTER, CENTER);
    textSize(30);
    fill(255);
    text(lines[i], posX, interY);
  }
}

```

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 18, 2019, 1:45pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/14 "2019-07-18T13:45:36Z")

</div>

You should join based on commas. And split based on commas.

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 18, 2019, 2:30pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/15 "2019-07-18T14:30:58Z")

</div>

Thanks for your patience!  
Now it looks better, but it doesn’t sort each word like it should so words don’t move  
as they should: 1rst line: “folly”, 2nd below: “for”, then 3rd below : “is”  
and so on.  
How can I split now and sort the words from the String?  
Thanks a lot,  
L

```auto

String wordsList="folly for to what is the word";
String message;
String []lines={"folly ", "folly for to ", "for to ", "what is the word"};
boolean drawText;
PFont font;

// *****************************************************************************************************************************************

void setup() {
  size(1080, 1600);
  font = createFont("helvetica", 30);
  textFont(font);

  for (int i = 0; i < lines.length-1; i++) {
    lines[i] = lines[i]+",";
  }
  // lines = loadStrings("Beckett.txt"); //link to the text
  message = join(lines, ","); // join all the words together
  lines = split(message, ","); //split the text line by line
  //lines = split(message, " ");
  println("Before sorting:");
  printArray(lines);
  java.util.Arrays.sort(lines);
  println("After sorting:");
  printArray(lines);
}

// *****************************************************************************************************************************************

void draw() {
  background(0);  
  translate(100, 100);
  smooth();
  int posX = 0;
  int posY = 0;

  for (int i=0; i<lines.length; i++) {
    int index = wordsList.indexOf(lines[i]);
    if (index<0)continue;

    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortX = 0;
    float interX = lerp(posX, sortX, m);
    float sortY =index*30+30;
    float interY = lerp(posY, sortY, m);
    posX +=textWidth(lines[i]);
    
    if ( lines.equals(" ")|| lines[i].indexOf(" ")>0) {
      posX=0; 
      posY+= 50;
    }
    textSize(30);
    fill(255);
    text(lines[i], posX, interY);
  }
}

```

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 18, 2019, 3:15pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/16 "2019-07-18T15:15:33Z")

</div>

Sort. Then add commas then join then split.

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 18, 2019, 3:36pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/17 "2019-07-18T15:36:31Z")

</div>

Dear @InferNova,

> Sort?  
> How can I sort if I don’t join and split before?  
> In this case I sort the sentences only, not each word separately…

```auto
void setup() {
  size(1080, 1600);
  font = createFont("helvetica", 30);
   textFont(font);

   println("Before sorting:");
   printArray(lines);
  java.util.Arrays.sort(lines);
  println("After sorting:");
  printArray(lines);
  for (int i = 0; i < lines.length-1; i++) {
    lines[i] = lines[i]+",";
  }
  message = join(lines, ","); // join all the words together
  lines = split(message, ","); //split the text line by line
}

```

Can you tell me more please?! Thanks, L

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [July 19, 2019, 1:19am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/18 "2019-07-19T01:19:41Z")

</div>

Your array is already filled. With the items. You wanna sort then join then split.

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 19, 2019, 7:14am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/19 "2019-07-19T07:14:59Z")

</div>

Hey @InferNova,  
Thank you for your message.  
I think I am not very clear. Please find below the same code but with char() instead of String(). I’d like to achieve exactly the same thing with Strings than with char…  
I hope now you see what I mean?!  
Thanks a lot!

```auto
import java.util.Calendar;
PFont font;
String[]lines;
String joinedText; 
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜß,.;:!? ";
int[] counters = new int[alphabet.length()];
boolean[] drawLetters = new boolean[alphabet.length()];
float charSize;
color charColor = 0;
int posX = 20;
int posY = 50;
boolean drawLines = false;
boolean drawText = true;
String list;

// ********************************************************************************************************************************************************

void setup() {
  size(1200, 1920);
  lines = loadStrings("Beckett.txt"); //laden des zu analysierenden textes
  joinedText = join(lines, " ");
  lines= split(joinedText, ",");
  font = createFont("helvetica", 25);
  textFont(font);
  for (int i = 0; i < alphabet.length(); i++) {
    drawLetters[i] = true;
  }
}

// ********************************************************************************************************************************************************

void draw() {
  background(0);
  translate (50, 0);
  noStroke();
  smooth();
  posX = 10;
  posY = 100;
  float oldX = 0;
  float oldY = 0;

  String [] words = split(joinedText, " ");
  // go through all characters in the text to draw them  
  for (int i = 0; i < joinedText.length(); i++) {
    // again, find the index of the current letter in the alphabet
    String s = str(joinedText.charAt(i)).toUpperCase();
    char uppercaseChar = s.charAt(0);
    int index = alphabet.indexOf(uppercaseChar);
    if (index < 0) continue;
    fill(255, 200);
    //textSize(30);
    float sortX = index*20+40;
    float sortY = index*20+40;
    float m = map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float interY = lerp(posY, sortY, m);
    float interX = lerp(posX, sortX, m);
    if (drawLetters[index]) {
      if (drawLines) {
        if (oldX!=0 && oldY!=0) {
          noFill();
          stroke(181, 157, 0, 100);
          strokeWeight(2);
          bezier(oldX, oldY, oldX+100*noise(oldX*150), oldY-30*noise(oldY*500), oldX+20*noise(interY*500), oldY+50*noise(oldX*150), interX, interY);
        }
        oldX = posX;
        oldY = interY;
      }
      if (drawText && uppercaseChar !=',') 
        text(joinedText.charAt(i), posX, interY);
    } else {
      oldX = 0;
      oldY = 0;
    }
    posX += textWidth(joinedText.charAt(i));
    if (uppercaseChar ==',') {
      posY += 20;
      if (i>0) {
        posX = 0;
      } 
      if (i>945) {
        posY+=40;
        posX=-10;
      }
    }
  }
}

// ********************************************************************************************************************************************************

void keyReleased() {

  if (key == '1') drawLines = !drawLines;
  if (key == '2') drawText = !drawText;

  if (key == '3') {
    for (int i = 0; i < alphabet.length(); i++) {
      drawLetters[i] = false;
    }
  }
  if (key == '4') {
    drawText = true;
    for (int i = 0; i < alphabet.length(); i++) {
      drawLetters[i] = true;
    }
  }
  String s = str(key).toUpperCase();
  char uppercaseKey = s.charAt(0);
  int index = alphabet.indexOf(uppercaseKey);
  if (index >= 0) {
    drawLetters[index] = !drawLetters[index];
  }
}

```

---

<div class="post-metadata">

### Author: ![lolonulu](https://avatars.discourse-cdn.com/v4/letter/l/ad7895/32.png) [@lolonulu](https://discourse.processing.org/u/lolonulu)
#### Post date: [July 19, 2019, 11:03am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/20 "2019-07-19T11:03:39Z")

</div>

Dear @GoToLoop and @Chrisir,

@InferNova helped me, but now I am lost in transtaion!?  
Please find here below a code with Char() replacing the Strings(). I would like to do exactly the same than in this sketch but with strings. Can you please help me?!!  
Thanks a lot in advance.  
Best, L

```auto
boolean drawText = true;
String list;

// ********************************************************************************************************************************************************

void setup() {
  size(1200, 1920);
  lines = loadStrings("Beckett.txt"); //laden des zu analysierenden textes
  joinedText = join(lines, " ");
  lines= split(joinedText, ",");
  font = createFont("helvetica", 25);
  textFont(font);
  for (int i = 0; i < alphabet.length(); i++) {
    drawLetters[i] = true;
  }
}

// ********************************************************************************************************************************************************

void draw() {
  background(0);
  translate (50, 0);
  noStroke();
  smooth();
  posX = 10;
  posY = 100;
  float oldX = 0;
  float oldY = 0;

  String [] words = split(joinedText, " ");
  // go through all characters in the text to draw them  
  for (int i = 0; i < joinedText.length(); i++) {
    // again, find the index of the current letter in the alphabet
    String s = str(joinedText.charAt(i)).toUpperCase();
    char uppercaseChar = s.charAt(0);
    int index = alphabet.indexOf(uppercaseChar);
    if (index < 0) continue;
    fill(255, 200);
    //textSize(30);
    float sortX = index*20+40;
    float sortY = index*20+40;
    float m = map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float interY = lerp(posY, sortY, m);
    float interX = lerp(posX, sortX, m);
    if (drawLetters[index]) {
      if (drawLines) {
        if (oldX!=0 && oldY!=0) {
          noFill();
          stroke(181, 157, 0, 100);
          strokeWeight(2);
          bezier(oldX, oldY, oldX+100*noise(oldX*150), oldY-30*noise(oldY*500), oldX+20*noise(interY*500), oldY+50*noise(oldX*150), interX, interY);
        }
        oldX = posX;
        oldY = interY;
      }
      if (drawText && uppercaseChar !=',') 
        text(joinedText.charAt(i), posX, interY);
    } else {
      oldX = 0;
      oldY = 0;
    }
    posX += textWidth(joinedText.charAt(i));
    if (uppercaseChar ==',') {
      posY += 20;
      if (i>0) {
        posX = 0;
      } 
      if (i>945) {
        posY+=40;
        posX=-10;
      }
    }
  }
}

// ********************************************************************************************************************************************************

void keyReleased() {

  if (key == '1') drawLines = !drawLines;
  if (key == '2') drawText = !drawText;

  if (key == '3') {
    for (int i = 0; i < alphabet.length(); i++) {
      drawLetters[i] = false;
    }
  }
  if (key == '4') {
    drawText = true;
    for (int i = 0; i < alphabet.length(); i++) {
      drawLetters[i] = true;
    }
  }
  String s = str(key).toUpperCase();
  char uppercaseKey = s.charAt(0);
  int index = alphabet.indexOf(uppercaseKey);
  if (index >= 0) {
    drawLetters[index] = !drawLetters[index];
  }
}

```

[Next page](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767.md?page=2)
