a good algorithm for sorting texts?

D

DAXU

Hi,
I am trying to find a good sorting algorithm to sort words in a
sentence alphabetically? can anyone give me some help?
many thanks
 
P

Peter Duniho

DAXU said:
Hi,
I am trying to find a good sorting algorithm to sort words in a
sentence alphabetically? can anyone give me some help?

Array.Sort() is a good place to start. Then, you might look at
List<T>.Sort().

http://msdn.microsoft.com/en-us/library/system.array.sort.aspx
http://msdn.microsoft.com/en-us/library/3da4abas.aspx

If you can define "words" better, then it's possible a suggestion can be
provided for how to divide a sentence into words. You might look at the
String.Split() method as a start though:
http://msdn.microsoft.com/en-us/library/system.string.split.aspx
 
M

Michael C

DAXU said:
Hi,
I am trying to find a good sorting algorithm to sort words in a
sentence alphabetically? can anyone give me some help?
many thanks

If you're using dot net framework 3.5 then linq is about as simple a
solution as you can get

string x = "the quick brown fox jumps over the lazy dog";
string[] y = x.Split(' ').OrderBy(i => i).ToArray();
 
F

Family Tree Mike

DAXU said:
Hi,
I am trying to find a good sorting algorithm to sort words in a
sentence alphabetically? can anyone give me some help?
many thanks

If you're using dot net framework 3.5 then linq is about as simple a
solution as you can get

string x = "the quick brown fox jumps over the lazy dog";
string[] y = x.Split(' ').OrderBy(i => i).ToArray();

If the sentence has punctuation, then you might need to do something
like this:

string x = "The quick, brown fox jumps over the very lazy dog.";
var y = x.Split(new char [] {' ', '.', ',', '!'},
StringSplitOptions.RemoveEmptyEntries).OrderBy(i => i);
 
P

Peter Duniho

Michael said:
DAXU said:
Hi,
I am trying to find a good sorting algorithm to sort words in a
sentence alphabetically? can anyone give me some help?
many thanks

If you're using dot net framework 3.5 then linq is about as simple a
solution as you can get

string x = "the quick brown fox jumps over the lazy dog";
string[] y = x.Split(' ').OrderBy(i => i).ToArray();

But inefficient.

It's one thing if all you've got is an IEnumerable<T> and no list-like
random access.

But if you're starting with an array in the first place, using the
Array.Sort() method is much more efficient than having to make a
complete copy of the original data and then copying it all back to yet
another new array.

It's simple syntax, but I wouldn't get in the habit of using LINQ to
sort arrays.

Pete
 
M

Michael C

Peter Duniho said:
But inefficient.

When I saw your name in the replies I was wondering what negative comments
you would come up with :)
It's one thing if all you've got is an IEnumerable<T> and no list-like
random access.

But if you're starting with an array in the first place, using the
Array.Sort() method is much more efficient than having to make a complete
copy of the original data and then copying it all back to yet another new
array.

Who says we are starting with an array?
It's simple syntax, but I wouldn't get in the habit of using LINQ to sort
arrays.

In the very rare cases where performance could be an issue then obviously
you would take appropriate action.

Michael
 
P

Peter Duniho

Michael said:
Who says we are starting with an array?

It was implied by the original poster, and was specifically called out
as such in your reply.
In the very rare cases where performance could be an issue then obviously
you would take appropriate action.

The array scenario is common enough, and trivial enough to address, that
it's an example of using the right tool for the job, rather than blindly
applying LINQ to everything.

Pete
 
M

Michael C

Peter Duniho said:
It was implied by the original poster, and was specifically called out as
such in your reply.

Both of those are incorrect. The OP makes no mention of starting with an
array and I start with a string.
The array scenario is common enough, and trivial enough to address, that
it's an example of using the right tool for the job, rather than blindly
applying LINQ to everything.

Who said I was applying linq to everything, I was just supplying a possible
solution. You really are trying your best to find something wrong. Do you
feel threatened that you didn't suggest this solution?

Michael
 
P

Peter Duniho

Michael said:
Both of those are incorrect. The OP makes no mention of starting with an
array and I start with a string.

Once you called String.Split(), you have an array.

As for whether the OP makes mention of an array, you seem to have
misunderstood the use of the word "implied". An implication, by
definition, is not stated explicitly.

You are free to your interpretation of what's implied by the original
problem statement, but clearly I do not agree with your assertion if
it's that an array wasn't implied by the original problem statement.
Given the use of String.Split(), an array is the obvious implementation.

As for your psychobabble…whatever. I'm simply trying to help the OP.
Stop taking it so personally. It's not always about you.

Pete
 
M

Michael C

Peter Duniho said:
Once you called String.Split(), you have an array.

Great, but I did not start with an array. I also only posted that method to
keep things simple. For this sort of thing I have a function that will split
the string as an IEnumerable.

string x = "the quick brown fox jumps over the lazy dog";
string[] y = x.SplitAsEnumerable(' ').OrderBy(i => i).ToArray();
As for whether the OP makes mention of an array, you seem to have
misunderstood the use of the word "implied". An implication, by
definition, is not stated explicitly.

There was nothing implied. You just made that bit up.
You are free to your interpretation of what's implied by the original
problem statement, but clearly I do not agree with your assertion if it's
that an array wasn't implied by the original problem statement.

I'm sure you do disagree with me, but I thought we established that long
ago.
Given the use of String.Split(), an array is the obvious implementation.

It's one possible implementation which has advantages and disadvantages.
As for your psychobabble…whatever. I'm simply trying to help the OP.

Maybe you're trying to do that but usually you're trying to make yourself
feel superior.

Michael
 
P

Peter Duniho

Michael said:
[...]
Maybe you're trying to do that but usually you're trying to make yourself
feel superior.

Again: whatever.

You have absolutely no clue as to what I'm trying to do, usually or
otherwise. Quit acting like you do.
 
M

Michael C

Peter Duniho said:
Again: whatever.

You have absolutely no clue as to what I'm trying to do, usually or
otherwise. Quit acting like you do.

It appears I have more of a clue than you do. As soon as I saw your name on
this thread you did pretty much *everything* I expected you would. I was
actually considering writing in my first post "I know pete won't like this
but here's a way to do it using linq".

Michael
 
P

Peter Duniho

Michael said:
It appears I have more of a clue than you do. As soon as I saw your name on
this thread you did pretty much *everything* I expected you would. I was
actually considering writing in my first post "I know pete won't like this
but here's a way to do it using linq".

Even assuming you accurately predicted my actions (of which I'm
skeptical), that in no way suggests that you comprehend the
_motivations_ for my actions, of which you pretend knowledge and which
is the pertinent point here.
 
M

Michael C

Peter Duniho said:
Even assuming you accurately predicted my actions (of which I'm
skeptical), that in no way suggests that you comprehend the _motivations_
for my actions, of which you pretend knowledge and which is the pertinent
point here.

Deny all you like pete, possibly you even deny to yourself.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top