# 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:** 2

<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, 11:42am UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/21 "2019-07-19T11:42:18Z")

</div>

Your splitting logic etc. Should still be handled in setup.

---

<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, 12:25pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/22 "2019-07-19T12:25:40Z")

</div>

Dear @interNova,  
Here is what I managed to do… Still not that!!  
Why only few words are moving according to mouseX!!!  
'm getting nut!  
Thanks

```auto
PFont font;
String[]lines;
String joinedText; 
String wordsList = "afaint afar all away folly for from given glimpse here is need over see seeing seem that the there this to what word";
int posX = 0;
int posY = 0;
boolean drawLines = false;
boolean drawText = true;

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

void setup() {
  size(1200, 1920);
  font = createFont("helvetica", 20);
  textFont(font);
  lines = loadStrings("Beckett.txt"); 
  println("Before sorting:");
  printArray(lines);
  //java.util.Arrays.sort(lines);
  println("After sorting:");
  printArray(lines);
  for (int i = 0; i < lines.length; i++) {
    lines[i]=lines[i]+',';
  }
  joinedText = join(lines, ",");
  lines= split(joinedText, ",");
}

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

void draw() {
  background(0);
  translate (50, 50);
  smooth();
  posX = 0;
  posY = 0;    
  for (int i = 0; i < lines.length; i++) {
    if ( lines[i].equals(" ")|| lines[i].indexOf(" ")>0) {
      posY += 20;
      posX = 0;
    }
    int index = wordsList.indexOf(lines[i]);
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortY =index*10+30;
    float interY = lerp(posY, sortY, m);
    fill(255, 200);
    text(lines[i], posX, interY);
    posX += textWidth(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 19, 2019, 3:31pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/23 "2019-07-19T15:31:46Z")

</div>

> [@InferNova](#):
>
> Your splitting logic etc. Should still be handled in setup.

Isn’t that the case?! sort+join+split are there…

---

<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 22, 2019, 4:25pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/24 "2019-07-22T16:25:02Z")

</div>

Dear @GoToLoop and @Chrisir,

Sorry to bother once again with my stupid problem. I am still stuck despite @InferNova 's help.  
I don’t understand why juts a part of the words are sorted and not the others?!!  
Could you please help me?  
Thank you very much in advance.  
L

```auto
PFont font;
String[]lines;
String joinedText; 
String wordsList = "afaint afar all away folly for from given glimpse here is need over see seeing seem that the there this to what word";
int posX = 0;
int posY = 0;
boolean drawLines = false;
boolean drawText = true;

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

void setup() {
  size(1200, 1920);
  font = createFont("helvetica", 20);
  textFont(font);
  lines = loadStrings("Beckett.txt"); 
  println("Before sorting:");
  printArray(lines);
  //java.util.Arrays.sort(lines);
  println("After sorting:");
  printArray(lines);
  //wordsList=sort(wordsList);
  for (int i = 0; i < lines.length; i++) {
    lines[i]=lines[i]+",";
  }
  joinedText = join(lines, ",");
  lines= split(joinedText, ",");
}

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

void draw() {
  background(0);
  translate (50, 50);
  smooth();
  posX = 0;
  posY = 0;  
  
  
    for (int j = 0; j < lines.length; j++) {
    int index = wordsList.indexOf(lines[j]);
    if(index<0) index=0;
     
    float m=map(mouseX, 50, width-50, 0, 1);
    m = constrain(m, 0, 1);
    float sortY =index*10;
    float interY = lerp(posY, sortY, m);
    
    if (joinedText.equals(" ")|| joinedText.indexOf(" ")>0) {
      posY += 10;
      posX = 0;
    }
    fill(255, 200);
    text(lines[j], posX, interY);
   posX += textWidth(lines[j]);
    
  }
}

```

---

<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 22, 2019, 7:31pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/25 "2019-07-22T19:31:14Z")

</div>

> [@lolonulu](#):
>
> 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?!!

Doesn’t run.

joinedText not declared, font not declared… many more…

You posted it wrong maybe?

* * *

**main sketch**

for the main sketch you want to improve: `Beckett.txt` missing

---

<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 22, 2019, 7:42pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/26 "2019-07-22T19:42:57Z")

</div>

I think there’s large confusion here. The sorting works. The displaying of the letters too then what doesn’t ?

---

<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 22, 2019, 7:47pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/27 "2019-07-22T19:47:35Z")

</div>

> [@lolonulu](#):
>
> I don’t understand why juts a part of the words are sorted and not the others?!!

that’s what he said

Chrisir

---

<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 22, 2019, 7:49pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/28 "2019-07-22T19:49:57Z")

</div>

I ran the code using the array and sorting it and everything worked fine. Or so it seemed.

---

<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 22, 2019, 8:22pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/29 "2019-07-22T20:22:59Z")

</div>

this works for me.

Maybe a , comma is just not suited as a separator since it might appear in the text itself and mixes everything up? Instead try # or so?

```auto
PFont font;
String[]lines={  
  "We are all born mad. Some remain so.", 
  "Dance first. Think later. It's the natural order.", 
  "All of old. Nothing else ever. Ever tried. Ever failed. No matter. Try again. Fail again. Fail better.", 
  "You're on Earth. There's no cure for that.", 
  "The tears of the world are a constant quantity. For each one who begins to weep somewhere else another stops. The same is true of the laugh.", 
  "Try again. Fail again. Fail better.", 
  "I can't go on, I'll go on.", 
  "Estragon: We always find something, eh Didi, to give us the impression we exist?", 
  "Every word is like an unnecessary stain on silence and nothingness.", 
  "The sun shone, having no alternative, on the nothing new.", 
  "My mistakes are my life.", 
  "I'm like that. Either I forget right away or I never forget.", 
  "Don't touch me! Don't question me! Don't speak to me! Stay with me!", 
  "The end is in the beginning and yet you go on.", 
  "Vladimir: Did I ever leave you?", 
  "Nothing is funnier than unhappiness.", 
  "Nothing happens. Nobody comes, nobody goes. It's awful.", 
  "I use the words you taught me. If they don't mean anything any more, teach me others. Or let me be silent.", 
  "The earth makes a sound as of sighs and the last drops fall from the emptied cloudless sky. A small boy, stretching out his hands and looking up at the blue sky, asked his mother how such a thing was possible. Fuck off, she said.", 
  "Let's go. - We can't. - Why not? - We're waiting for Godot.", 
  "Perhaps my best years are gone. When there was a chance of happiness. But I wouldn't want them back. Not with the fire in me now. No, I wouldn't want them back.", 
  "Memories are killing. So you must not think of certain things, of those that are dear to you, or rather you must think of them, for if you don’t there is the danger of finding them, in your mind, little by little.", 
  "Let us do something, while we have the chance! It is not every day that we are needed. Not indeed that we personally are needed. Others would meet the case equally well, if not better. To all mankind they were addressed, those cries for help still ringing in our ears! But at this place, at this moment of time, all mankind is us, whether we like it or not. Let us make the most of it, before it is too late! Let us represent worthily for one the foul brood to which a cruel fate consigned us! What do you say? It is true that when with folded arms we weigh the pros and cons we are no less a credit to our species. The tiger bounds to the help of his congeners without the least reflexion, or else he slinks away into the depths of the thickets. But that is not the question. What are we doing here, that is the question. And we are blessed in this, that we happen to know the answer. Yes, in the immense confusion one thing alone is clear. We are waiting for Godot to come -- ", 
  "There’s man all over for you, blaming on his boots the faults of his feet.", 
  "Nothing is more real than nothing.", 
  "You must go on. I can't go on. I'll go on.", 
  "ESTRAGON: I can't go on like this. VLADIMIR: That's what you think.", 
  "Words are all we have.", 
  "The only sin is the sin of being born", 
  "That's how it is on this bitch of an earth."
};

// String joinedText; 

//boolean drawLines = false;
//boolean drawText = true;

String[] lines2; 

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

void setup() {
  size(1200, 920);
  font = createFont("helvetica", 20);
  textFont(font);

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

  lines2 = new String[lines.length];  
  for (int i = 0; i < lines.length; i++) {
    lines[i] = lines[i].trim();
    if (!lines[i].trim().equals("")) 
      lines2[i]=lines[i].trim();
  }

  java.util.Arrays.sort(lines2);

  println("lines +++++++++++++++++++++++++++++++++++++++++++++++++");
  printArray(lines);
  println("lines2 +++++++++++++++++++++++++++++++++++++++++++++++++"); 
  for (int i = 0; i < lines.length; i++) {
    println( lines2[i]);
  }
}

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

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

  int lineDist= 20; 

  for (int j = 0; j < lines.length; j++) {

    int index = givePosition(lines2, lines[j] );

    if (index<0)
      index=0;

    float m=map(mouseX, 0, width, 
      -0.2, 1.2);
    m = constrain(m, 0, 1);
    float sortY = index*lineDist+20;
    float interY; 
    interY = lerp(posY, sortY, m);

    fill(255, 200);
    text(lines[j], 
      posX, interY);

    posY += lineDist;
    posX = 0;
  }
}

int givePosition( String[] list, String search ) {

  for (int j = 0; j < list.length; j++) {
    if (list[j].equals(search))
      return j;
  }//for

  print("f"); 
  return 0;
}
//

```

---

<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 22, 2019, 8:24pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/30 "2019-07-22T20:24:12Z")

</div>

I completely agree maybe commas are not the way to go # seems more suiting.  
Sorry this is out of loop but where is this quote from ? XD Love it ( Especially the last line. )

edit: I know where this is from ! It from that Game of Thrones actor giving a speech on his journey of becoming an actor I think. Well it seems like a mix !

---

<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 22, 2019, 8:26pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/31 "2019-07-22T20:26:33Z")

</div>

You lack knowledge, young padawan

Author Samuel Beckett

> **[Samuel Beckett Quotes (Author of Waiting for Godot)](https://www.goodreads.com/author/quotes/1433597.Samuel_Beckett)**
>
> 908 quotes from Samuel Beckett: 'We are all born mad. Some remain so.', 'Dance first. Think later. It's the natural order.', and 'All of old. Nothing else ever. Ever tried. Ever failed. No matter. Try again. Fail again. Fail better.'

---

<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 22, 2019, 8:27pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/32 "2019-07-22T20:27:36Z")

</div>

Aha ! So that where the guy must have got it ! Well thank you this is quite interesting !

Thank you Jedi @Chrisir

---

<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 22, 2019, 8:29pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/33 "2019-07-22T20:29:11Z")

</div>

His text file is called Beckett but we don’t have it…

---

<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 22, 2019, 8:33pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/34 "2019-07-22T20:33:10Z")

</div>

> [@lolonulu](#):
>
> int index = wordsList.indexOf(words[j]);

I don’t fully understand the way you are trying to do this.

But this line for example would give you the number of the char where the phrase occurs. E.g. 310.

But this is the number of the letter, it is **not** the number of the quote / line imo.

So I changed that and wrote a function.

---

<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 22, 2019, 9:22pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/35 "2019-07-22T21:22:07Z")

</div>

Thank you very much for your help jedi @InferNova . I really appreciate your laser sword attack!!

---

<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 22, 2019, 9:58pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/36 "2019-07-22T21:58:26Z")

</div>

Thank you @Chrisir Skywalker! Yes commas were certainly not appropriate!?  
Thank you so much to you and @InferNova you made my week!!  
And since we talk about Beckett, here is the text I want to use, it’s his last poem, a way to tell  
you don’t how to tell! THANK YOUUUU VERY MUCH

```auto
PFont font;
String[]lines={  
"folly",
"folly for to",
"for to",
"what is the word",
"folly from this",
"all this",
"folly from all this",
"given",
"folly given all this",  
"seeing",
"folly seeing all this",
"this",
"what is the word",
"this this",
"this this here",
"all this this here",
"folly given all this",
"seeing",
"folly seeing all this this here",
"for to",
"what is the word", 
"see",
"glimpse",
"seem to glimpse",
"need to seem to glimpse",
"folly for to need to seem to glimpse",
"what",
"what is the word",
"and where",
"folly for to need to seem to glimpse what where", 
"where",
"what is the word", 
"there",
"over there",
"away over there",
"afar",
"afar away over there",
"afaint",
"afaint afar away over there what",
"what",
"what is the word",
"seeing all this",
"all this this",
"all this this here",
"folly for to see what",
"glimpse",
"seem to glimpse",
"need to seem to glimpse",
"afaint afar away over there what",
"folly for to need to seem to glimpse afaint afar away over there what", 
"what",
"what is the word", 
"what is the word"
};

//boolean drawLines = false;
//boolean drawText = true;

String[] lines2; 

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

void setup() {
  size(1200, 920);
  font = createFont("helvetica", 20);
  textFont(font);

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

  lines2 = new String[lines.length];  
  for (int i = 0; i < lines.length; i++) {
    lines[i] = lines[i].trim();
    if (!lines[i].trim().equals("")) 
      lines2[i]=lines[i].trim();
  }
  
  java.util.Arrays.sort(lines2);

  println("lines +++++++++++++++++++++++++++++++++++++++++++++++++");
  printArray(lines);
  println("lines2 +++++++++++++++++++++++++++++++++++++++++++++++++"); 
  for (int i = 0; i < lines.length; i++) {
    println( lines2[i]);
  }
}

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

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

  int lineDist= 20; 

  for (int j = 0; j < lines.length; j++) {

    int index = givePosition(lines2, lines[j] );

    if (index<0)
      index=0;

    float m=map(mouseX, 0, width, 
      -0.2, 1.2);
    m = constrain(m, 0, 1);
    float sortY = index*lineDist+20;
    float interY; 
    interY = lerp(posY, sortY, m);

    fill(255, 200);
    text(lines[j], 
      posX, interY);

    posY += lineDist;
    posX = 0;
  }
}

int givePosition( String[] list, String search ) {

  for (int j = 0; j < list.length; j++) {
    if (list[j].equals(search))
      return j;
  }//for

  print("f"); 
  return 0;
}

```

---

<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 22, 2019, 10:02pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/37 "2019-07-22T22:02:02Z")

</div>

In fact I don’t want to sort each phrase/sentence as you did, but each word, that’s why I wrote the list of all the words contained in the poem in the alphabetical order. An with this line

`int index = wordsList.indexOf(words[j]);`

I wanted to get access to each word… Thank you very much for your help!!

---

<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 22, 2019, 10:13pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/38 "2019-07-22T22:13:24Z")

</div>

Did you try?

Please post your recent version

there is no comma in the poem. Why did you agree a comma is not appropriate?

> but each word

There won’t be enough lines to do this. Do you want to see each word only once (when it occurs let’s say 3 times?) or multiple times?

---

<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 22, 2019, 10:28pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/39 "2019-07-22T22:28:37Z")

</div>

> there is no comma in the poem. Why did you agree a comma is not appropriate?

Because initially I’ve added some commas to the poem…  
No I thought to see each word as many times it occurs in the poem and the to keep their posX coordinates and change their Y coordinates… I will try tomorrow. Many thanks

---

<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 22, 2019, 10:35pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/40 "2019-07-22T22:35:35Z")

</div>

Still doesn’t make much sense

You want only one word per line??

Then you need many lines…

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

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