How to sort words alphabetically from a String?

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