How to sort words alphabetically from a String?

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

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

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

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

1 Like

that’s what he said

Chrisir

1 Like

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

1 Like

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?

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;
}
//
2 Likes

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 !

1 Like

You lack knowledge, young padawan

Author Samuel Beckett

1 Like

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

Thank you Jedi @Chrisir

2 Likes

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

2 Likes

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.

1 Like

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

2 Likes

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

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

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!!

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?

2 Likes

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

1 Like

Still doesn’t make much sense

You want only one word per line??

Then you need many lines…

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