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

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

</div>

No I’d like for instance the word ‘this’ is repeated 16 times at different posX I’d like the 16 ‘this’ to be on the same line at the same posY coordinate but each one at their original posX positions  
like:  
this this this this this this  
thank you verrrry much

Laurent Mareschal  
[https://laurentmareschal.com](https://laurentmareschal.com)

> **[laurent mareschal (@l.mareschal) • Instagram photos and videos](https://www.instagram.com/l.mareschal/)**
>
> 263 Followers, 703 Following, 136 Posts - See Instagram photos and videos from laurent mareschal (@l.mareschal)

  
P +33 (0)6.65.62.44.59

----- Mail d’origine -----

---

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

</div>

Dear @Chrisir,  
This code below is just slightly modified to show that all the similar words are on the same line in posY when mouseX = width. Now I need to find how to display it well  
that when mouseX is 0 then the sentences should be display like the original text.I hope now you see what I mean ?! Thank you very much. Best, L

```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"
};
String wordsList;
//boolean drawLines = false;
//boolean drawText = true;

String[] lines2; 

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

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

  /* for (int i = 0; i < lines.length; i++) {
   lines[i]=lines[i]+"#";
   }*/
  wordsList = join(lines, " ");
  lines= splitTokens(wordsList, " ");

  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+10;
    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:** ![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 23, 2019, 6:03pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/43 "2019-07-23T18:03:43Z")

</div>

Is this solved?

What’s missing?

---

<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 23, 2019, 6:45pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/44 "2019-07-23T18:45:18Z")

</div>

I am glad you managed to copy the poem into the code finally. 😉

---

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

</div>

No… As u can see if you run the sketch, the words are sorted one by one and well positioned when mouseX=width but they are one per line instead of forming the sentences when the sketch begins and when mousex=0!  
In the seketch you sort the whole lines and Id’like to sort the words instead. Should I change this function?

```auto
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 23, 2019, 6:46pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/46 "2019-07-23T18:46:12Z")

</div>

yes thanks a lot ;))

---

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

</div>

> [@lolonulu](#):
>
> This code below is just slightly modified

Can I work from this code or shall I start with one of the previous versions?

---

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

</div>

This one is fine! Thanks a lot

---

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

</div>

solved!!!

no, just kidding

I don’t have time, but I think what you have to do is skip all the split and join, because you want to work with the poem.

Now, since you want to work with single words, you want to have a 2nd for loop, going over the words within the current line of the poem and handle the word

Can’t do that but wish you luck.

Chrisir

```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"
};
String wordsList;
//boolean drawLines = false;
//boolean drawText = true;

String[] lines2; 

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

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

  /* for (int i = 0; i < lines.length; i++) {
   lines[i]=lines[i]+"#";
   }*/
  //wordsList = join(lines, " ");
  //lines= splitTokens(wordsList, " ");

  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+10;
    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 23, 2019, 6:59pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/50 "2019-07-23T18:59:18Z")

</div>

Since I have a very tired brain and very few knowledge in code, I will need a lot of luck 😂🤪😬😰🥺😭 thank you master @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 23, 2019, 7:18pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/51 "2019-07-23T19:18:50Z")

</div>

How do you like the new version??

What’s wrong?

---

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

</div>

```auto

// not bad 

import java.util.Map;

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"
};

// --------------------------------------------------------

String[] lines2; 

String[] c1={};
String[] wordsList2;

// Note the HashMap's "key" is a String and "value" is an Integer
HashMap<String, Integer> hmYPos = new HashMap<String, Integer>();

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

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

  prepare();
}

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

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++) {

    String[]wordsInThatLine=split( lines[j], " "); 

    for (int j2 = 0; j2 < wordsInThatLine.length; j2++) {

      int index = hmYPos.get(wordsInThatLine [j2] ); // givePosition(lines2, lines[j] );
      float sortY = index; // index*lineDist+10;

      if (index<0)
        index=0;

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

      fill(255, 200);
      text(wordsInThatLine [j2], 
        posX, interY);

      posX+= textWidth( wordsInThatLine [j2] + " " ) + 3;

      //
    }//for
    posX = 0;
    posY += lineDist;
  }//for
}

//------------------------------------------------------------

void prepare() {
  HashMap<String, Integer> hm1 = new HashMap<String, Integer>();
  String message;

  message= join(lines, " ");
  lines2=split(message, ",");

  // Init 
  wordsList2 = split(join(lines, " "), " "); 
  for (String s : wordsList2) {
    hm1.put(s.toLowerCase(), 0); // all 0
  }

  // Counting 
  for (int i=0; i<lines2.length; i++) {
    String word = lines2[i].toLowerCase().trim();
    if (hm1.get(word) != null) {
      hm1.put(word, ((int) (hm1.get(word)))+1); // add 1
    }
  }

  //// Using an enhanced loop to iterate over each entry
  for (Map.Entry me : hm1.entrySet()) {
    c1 = (String[]) append (c1, me.getKey());
  }//for

  c1=sort(c1);

  // simulate output and save the line number for each word in sorted position 
  int posY=40;
  for (String s : c1) {
    hmYPos.put( s, posY); 
    posY += 20;
  }
}//
//

```

---

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

</div>

almost there, but the whole sentnces are sorted and still not the words!!!  
I try now with a 2nd for loop 🙂

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

```

But then  
`java.util.Arrays.sort(lines2)`  
doesn’t work with double loop!

---

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

</div>

okay u r ze Master!!🙇‍♂️  
Yes hashMap is great for that but since I just discovered it few days ago when you’ve sent me a counting test…! Thank you very, very, very much, this is exactly what I wanted to do! 👌😄

---

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

</div>

> [@Chrisir](#):
>
> int index = hmYPos.get(wordsInThatLine [j2] ); // givePosition(lines2, lines[j] );  
> float sortY = index;

that can be shortened

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

</div>

Even more elegant! Waaow I have so much to learn…  
If you can, could you please explain me a bit the prepare() function please?  
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 23, 2019, 8:31pm UTC](https://discourse.processing.org/t/how-to-sort-words-alphabetically-from-a-string/12767/57 "2019-07-23T20:31:09Z")

</div>

Sure

It’s preparing the sketch

😉

---

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

</div>

Let me see.

prepare() is indeed partly taken from the old HashMap sketch (see other thread). See reference for HashMap.

- We use a HashMap because it’s the easiest way to get to a list where each word occurs only once.

The function:

- first loop: pre-init hash-map with all 0

- 2nd loop count all occurrences. occurrences are never used but we have a list of all words now. Every word only **once**. Good.

- Third loop: copy hash-map to c1. Each word is in there only once. Nice.

- Sort c1.

- Last loop: Simulate the showing of sorted c1 and monitor the y pos for each word sorted. Store that position in a 2nd HashMap `hmYPos`: HashMap means we can get the y pos from the HashMap by using the word.

We use hmYPos in `draw()`, the main function.

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

</div>

new version

```auto
import java.util.Map;

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"
};

// Note the HashMap's "key" is a String and "value" is an Integer
HashMap<String, Integer> hmYPos = new HashMap<String, Integer>();

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

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

  prepare();
}

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

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++) { 
    // loop over the lines 

    // make array with the words in that line 
    String[]wordsInThatLine = split(lines[j], " "); 

    for (int j2 = 0; j2 < wordsInThatLine.length; j2++) { 
      // loop over the words in that line 

      // using the global HashMap to get the y pos of the sorted word
      int sortY = hmYPos.get(wordsInThatLine [j2] );  

      // getting mouse pos 
      float m=map(mouseX, 0, width, -0.2, 1.2);
      m = constrain(m, 0, 1);
      float interY = lerp(posY, sortY, m);

      fill(255);
      text(wordsInThatLine[j2], 
        posX, interY);

      posX+= textWidth( wordsInThatLine[j2] + " " ) + 3;
      //
    }//for
    posX = 0;
    posY += lineDist;
  }//for
}//func 

//------------------------------------------------------------

void prepare() {
  HashMap<String, Integer> hm1 = new HashMap<String, Integer>();
  String allLinesAsString;

  String[] lines2; 

  String[] words={};
  String[] wordsList2;

  // Init hm1
  wordsList2 = split(join(lines, " "), " "); 
  for (String s : wordsList2) {
    hm1.put(s.toLowerCase().trim(), 0); // all 0
  }

  allLinesAsString = join(lines, " ");
  lines2=split(allLinesAsString, " ");

  // Counting (filling hm1)
  for (int i=0; i<lines2.length; i++) {
    String word = lines2[i].toLowerCase().trim();
    if (hm1.get(word) != null) {
      hm1.put(word, ((int) (hm1.get(word)))+1); // add 1
    }
  }

  //// Using an enhanced loop to iterate over each entry of hm1
  for (Map.Entry me : hm1.entrySet()) {
    // copy hm1 to words 
    words = (String[]) append (words, me.getKey());
  }//for

  words=sort(words);

  // simulate output and save the line number for each word in sorted position 
  int posY=40;
  for (String s : words) {
    hmYPos.put( s, posY); 
    posY += 20;
  }
}//
//

```

---

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

</div>

Dear @Chrisir,

It’s just fantastic and adorable! Thank you so much for taking some of your precious time explaining me every

part of the sketch and congrats for the new improved version, simply gorgeous!

Thank you very much for your great help

Best,

L

Laurent Mareschal  
[https://laurentmareschal.com](https://laurentmareschal.com)

> **[laurent mareschal (@l.mareschal) • Instagram photos and videos](https://www.instagram.com/l.mareschal/)**
>
> 263 Followers, 703 Following, 136 Posts - See Instagram photos and videos from laurent mareschal (@l.mareschal)

  
P +33 (0)6.65.62.44.59

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

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