MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: SortBy + Sort Strings with apo.marks + CharacterCode

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118272] Re: SortBy + Sort Strings with apo.marks + CharacterCode
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Tue, 19 Apr 2011 06:57:56 -0400 (EDT)
  • References: <iofs9a$s2b$1@smc.vnet.net>

Sort by default uses the function OrderedQ to determine whether
elements in the list are ordered. This function also works for
strings. Therefore the standard sort works on your list of strings.
The '>' operator is not defined for strings. "Ape" > "Nut" returns
unevaluated, and so the sort with this ordering function doesn't work.
You'll have to write one yourself if the standard ordering is  not
sufficient.

Here's a quick approach just to give you an idea. No doubt there will
be better ones.

stringOrderedQ[s1_String, s2_String] :=
 Module[{ml = Min[StringLength /@ {s1, s2}], ss1, ss2, i = 1},
  {ss1, ss2} = ToCharacterCode[StringTake[#, ml]] & /@ {s1, s2};
  While[i <= ml && ss1[[i]] == ss2[[i]], i++];
  If[i <= ml, ss1[[i]] < ss2[[i]], StringLength[s1] <
StringLength[s2]]
  ]

In[53]:= Sort[{"January", "February", "March", "April", "May", "June",
   "July", "August", "September", "October", "November", "December",
  "English"}, stringOrderedQ]

Out[53]= {"April", "August", "December", "English", "February", \
"January", "July", "June", "March", "May", "November", "October", \
"September"}

As to your other questions. To be honest I don't have the faintest
idea what you mean by the sorting order abcccdde .

Cheers -- Sjoerd

More Mathematica questions answered at StackOverflow
http://stackoverflow.com/questions/tagged/mathematica


On Apr 18, 1:14=C2 am, "Dr Andy D Kucar P2EE4 www.radio4u.com"
<a... at radio4u.com> wrote:
> Dear Mathgroup,
>
> Following on the original SortBy post and responses,
> I would like to expand with the following (Ver.4) funs
>
> In[17]
> Sort[{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
> "December", "English"}]
> Sort[{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
> "December", "English"}, #1 > #2 & ]
> ToCharacterCode[{"A2Z a2z =C3=85 =C2 Cc?? Cc =C3=90 =C5 =C5=A1 \!\(Z\&?\)\!\(z\&?\)"}]
>
> Out[17]
> {"April", "August", "December", "English", "February", "January", "July","June", "March", "May", "November", "October",
> "September"}
> this is OK
>
> Out[18]
> {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
> "December", "English"}
> this is NOT ok; #1>#2 & has been ignored; the input has NOT been evaluated, i.e. sorted;
> why?
> How to Sort these String things?
>
> Out[19]
> {{65, 50, 90, 32, 97, 50, 122, 32, 197, 32, 268, 269, 32, 262, 263, 32, 208, 32, 352, 353, 32, 63425, 63433, 90, 63431,
> 780, 63424, 63425, 63433, 122, 63431, 780, 63424}}
> this ToCharacterCode conversion is OK;
> however, I have been using a text with some international characters, which require sorting rules different from the
> rule presented in Mathematica;
>
> For example, one would like to sort a particular String according to the following sorting rule: abcccdde ...
>
> One of (perhaps easiest) sorting possibilities would be, having been able to define a sorting rule,
> or redefine the CharacterCode and give it a real (rational) number,
> such as c (change in a program its CharacterCode from 269 to 99.1, or 99+269/1000, a CharacterCode number between c and
> d), etc.
>
> particularly problematic have been characters created via (Ctrl+& Esc hc Esc) construction
>
> how to create b- d-(d) h- characters ?
>
> thank you, sincerely andy
>
> -----Original Message-----
> From: graser
> Sent: Friday, April 15, 2011 02:57
> Subject: SortBy
>
> Dear Mathematica group,
>
> I have a simple question for you.
>
> Let's say there is a list like
>
> KS = {{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32, 1}};
>
> I want to sort it by second element.
>
> I can use
>
> Sort[KS, #2[[2]] > #1[[2]] &]
>
> It gives out like
>
> {{500, 23, 5}, {40, 32, 1}, {300, 48, 2}, {120, 55, 7}}
>
> But if I want to use SortBy, how to do that?
>
> SortBy[KS, ??]
>
> Thanks!
>
>



  • Prev by Date: Re: trouble printing to PDF
  • Next by Date: Form of Solutions to DSolve
  • Previous by thread: Re: SortBy + Sort Strings with apo.marks + CharacterCode
  • Next by thread: Re: SortBy + Sort Strings with apo.marks + CharacterCode