Hey guys, I've been in need of something that will sort a list of text by the amount of characters on each line. I figured PHP could be used to code something like this, and would probably be much easier then making a program that does it. Does anyone know where I should start with this? I'm not exactly the greatest PHP coder, so if anyone can help me with this or point me in the right direction- I'd appreciate it a lot. An example of what I'd like it to do is; I paste the following text or even load a .txt file containing this text into a typing area. aaaa aaaaaa aaa aaaaaaaa aa a aaaaaaa and that gets sorted into a aa aaa aaaa aaaaaa aaaaaaa aaaaaaaa Hope that helps you guys figure out what I'm lookin' for. Any help would be appreciated -Danny
Here's some psuedo code to help you visualize the solution Load the words into an array (hint: explode.. \n) Create a new array Loop until new array's length equals old array's length Start at length 0 go up When you find a string of the right length put it in the new array Print out new array
PHP's sort function will do the trick. It sorts lowest to highest. $data = array("aaa", "aaaa", "a", "aaaaaaa"); sort($data); foreach ($data as $key => $val) { echo "data[" . $key . "] = " . $val . "\n"; } PHP:
it is easier to do it in C++, you have 2 options, import all words into an array, sort the array with any sorting method you know, but don't compare strings, but their lenghts, and then rewrite the array to the file. Other option is to make the sorting directly in the file, without using the array.
cs.ubc.ca/~harrison/Java/sorting-demo.html just some algorithms to look at for sorting data like this.