How to sort words alphabetically from a String?

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


P +33 (0)6.65.62.44.59

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

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

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

Is this solved?

What’s missing?

1 Like

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

1 Like

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?

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

yes thanks a lot ;))

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

This one is fine! Thanks a lot

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


//

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;
}
//
1 Like

Since I have a very tired brain and very few knowledge in code, I will need a lot of luck :joy::crazy_face::grimacing::cold_sweat::pleading_face::sob: thank you master @Chrisir :wink:

How do you like the new version??

What’s wrong?



// 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;
  }
}//
//
1 Like

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

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!

okay u r ze Master!!:bowing_man:
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! :ok_hand::smile:

1 Like

that can be shortened

get rid of index

1 Like

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

1 Like

Sure

It’s preparing the sketch

:wink:

1 Like

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

new version

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;
  }
}//
//
1 Like

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


P +33 (0)6.65.62.44.59

1 Like