COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Decimal sort

• Email
• Follow

```Is there any way to sort floating point numbers in perl?  Every
example I have tried has failed.  Numerical sort appears to NOT to  be
a  numerical sort but rather an integer sort.

Thanks,

Marshall
```
 0

See related articles to this posting

```mdudley wrote:
> Is there any way to sort floating point numbers in perl?  Every
> example I have tried has failed.  Numerical sort appears to NOT to  be
> a  numerical sort but rather an integer sort.

Perhaps you are sorting them as strings instead of numbers?

John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway
```
 0

```mdudley wrote:
> Is there any way to sort floating point numbers in perl?  Every
> example I have tried has failed.  Numerical sort appears to NOT to  be
> a  numerical sort but rather an integer sort.
>

worksforme
perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
0.1
0.25
0.3
0.5
0.7
```
 0

```mdudley <mdudley@king-cart.com> wrote:
>Is there any way to sort floating point numbers in perl?  Every
>example I have tried has failed.  Numerical sort appears to NOT to  be
>a  numerical sort but rather an integer sort.

Please post a minimal working sample program including sampe data as
needed that demonstrates your problem. Because every example that I have
tried has worked just fine.

jue
```
 0

```Steve C wrote:
> mdudley wrote:
>> Is there any way to sort floating point numbers in perl?  Every
>> example I have tried has failed.  Numerical sort appears to NOT to  be
>> a  numerical sort but rather an integer sort.
>>
>

Oops.  Forgot about default sort function being string cmp.  Although that
tends to work in a surprising number of cases.

perl -e 'print join ("\n", sort {\$a <=> \$b} (0.1, 0.7, -0.3, 0.25, 0.5))'
-0.3
0.1
0.25
0.5
0.7
```
 0

```mdudley <mdudley@king-cart.com> wrote:

> Is there any way to sort floating point numbers in perl?  Every
> example I have tried has failed.  Numerical sort appears to NOT to
> be a  numerical sort but rather an integer sort.

I suspect that reading the docs for sort() will help.  There are even
examples.
```
 0

```Steve C <smallpond@juno.com> writes:

> mdudley wrote:
>> Is there any way to sort floating point numbers in perl?  Every
>> example I have tried has failed.  Numerical sort appears to NOT to  be
>> a  numerical sort but rather an integer sort.
>>
>
> worksforme
> perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
> 0.1
> 0.25
> 0.3
> 0.5
> 0.7

Coincidence. That's why you must read documentation...

perl -e 'print join("\n", sort ( 0.0001, 0.000002, 0.1 ))'
0.0001
0.1
2e-06

Note that sort defaults to string compare:

perl -e 'print join("\n", sort { \$a <=> \$b } ( 0.0001, 0.000002, 0.1 ))'
2e-06
0.0001
0.1

--
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development
```
 0

```Steve C <smallpond@juno.com> wrote:
> mdudley wrote:
>> Is there any way to sort floating point numbers in perl?  Every
>> example I have tried has failed.  Numerical sort appears to NOT to  be
>> a  numerical sort but rather an integer sort.
>>
>
> worksforme
> perl -e 'print join ("\n", sort ( 0.1, 0.7, 0.3, 0.25, 0.5))'
> 0.1
> 0.25
> 0.3
> 0.5
> 0.7

Doesn't work for me...

perl -e 'print join ("\n", sort (2.0, 12.0))'
12
2

Your's is not a numeric sort at all, it is a stringwise sort.

perl -e 'print join ("\n", sort {\$a <=> \$b} (2.0, 12.0))'
2
12

--
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
```
 0

7 Replies
1323 Views

Similar Articles

12/7/2013 12:26:31 AM
[PageSpeed]

Similar Artilces:

sort_by
Hello I have a list of rows which I want to order by a column. I make it with @values_ordered = @values.sort_by(&:id_column) What if I want to inverse the order (like order by x desc in SQL) How could I do that using sort_by? is that possible? thanks regards. -- Posted via http://www.ruby-forum.com/. On 17/10/2007, Paulo Carvalho <pjcarvalho@gmail.com> wrote: > I have a list of rows which I want to order by a column. > I make it with > @values_ordered = @values.sort_by(&:id_column) > > What if I want to inverse the order (like order by x desc in SQL) > > How could I do that using sort_by? is that possible? values_orderd = @values.sort_by{|value| -value.id_column} Hello Thanks for your quick answer. However i am having a problem. I used the syntax that you give to me like this : To order (ASC way): values_ordered = @demandes.sort_by{|value| value.eluxid} (This one works fine) To order (DESC way) values_ordered = @demandes.sort_by{|value| -value.eluxid} This one send to me a syntax error (I think he dont like the '-'): undefined method `-@' for ["3CC11A"]:Array (note: "3CC11A" is the column value

Selection sort and bubble sort
Selection sort and bubble sort have same performance always, right? Are the following correctly implemented the both functions. Comments are welcome. Selection sort an array of intergers range between index l and r in ascending order. For example, sort "312" into "123" void sort_sel(int a, int l, int r) { int i, j, n; for (i = l; i < r; i++) for (j = i + 1; j <= r; j++) if (a[i] > a[j]){ n = a[i]; a[i] = a[j]; a[j] = n; } } Bubble sort an array of intergers range between index l and r in ascending order. For example, sort "312" into "123" void sort_bub(int a, int l, int r) { int i, n; for (; l < r; r--) for (i = l; i < r; i++) if (a[i] > a[i + 1]){ n = a[i]; a[i] = a[i + 1]; a[i + 1] = n; } } lovecreatesbea...@gmail.com wrote On 10/16/07 15:32,: > Selection sort and bubble sort have same performance always, right? <off-topic> Wrong. </off-topic> > Are the following correctly implemented the both functions. Comments > are welcome. > > > Selection sort an array of intergers range between

Sub-sort after sort
Hello, I have been sorting a list of dicts using the following function: result_rs = sorted(unsort_rs, key=itemgetter(orderby)) and this works fine. Now I am looking to perform a subsort as well. For example, I have this: test = [{'name': 'John Smith', 'location': 'CA',},{'name': 'John Smith', 'location': 'AZ',},] I would want to sort by name first, then sub sort by location. Any ideas? Thanks! On Nov 2, 2:45 pm, kee...@gmail.com wrote: > Hello, I have been sorting a list of dicts using the following > function: > > result_rs = sorted(unsort_rs, key=itemgetter(orderby)) > > and this works fine. Now I am looking to perform a subsort as well. > For example, I have this: > > test = [{'name': 'John Smith', 'location': 'CA',},{'name': 'John > Smith', 'location': 'AZ',},] > > I would want to sort by name first, then sub sort by location. Any > ideas? Thanks! Well, I found a way to do it. Just create a new key and value that is a combination of two other values, like this: test = [{'name': 'John Smith

sort in FUP
hello is it easy way to sort files not by alphabet but by date? i need a variable with list of files but sorted by date any tip? anyway thx :) AFAIK, there is no command or builtin TACL function that provides this functionality. If you're lucky, someone will post with a sample macro. I have a sample program that lists files by modification date. Contact me via e-mail if you would like a sample. If you want to write your own utility, see the manuals related to TACL. Look at the builtin functions #NEXTFILENAME and #FILEINFO. The sort is up to you - there is a #SORT function but I think it's for a space separated list. If you want to write a program, see the Guardian procedure calls, FILENAME_FINDSTART_ and FILENAME_FINDNEXT_ and FILE_GETINFOLISTBYNAME_. Another option would be to use the FILEINFO command (or FUP INFO) sending the output to a file. Then use the SORT utility on that file. > AFAIK, there is no command or builtin TACL function that provides this > functionality. > > If you're lucky, someone will post with a sample macro. I have a > sample program that lists files by modification date. Contact me via > e-mail if you would like

Sort in Pages?
I've probably missed something really obvious but I can't seem to find the sort function in Pages--- you know, for lists and such (not the one for tables). I've been using the one in Services instead. What have I missed?

xsl:sort
Example XML: <?xml version="1.0" encoding="UTF-8" ?> <root> <foo> <index>1></index> <bar> <child /> <child /> </bar> </foo> <foo> <index>n</index> <bar> <child /> <child /> </bar> </foo> </root> My question is (as I understand pieces of how xsl:apply-templates works with xsl:sort) why does this work: <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="root"> <xsl:apply-templates> <xsl:sort select="index" order="descending" /> </xsl:apply-templates> </xsl:template> Since I haven't (at least without assuming) that I'm sorting 'foo' elements. Thanks for any input. Why do you think this shouldn't work? ===== Cheers, Dimitre Novatchev. http://fxsl.sourceforge.net/ -- the home of FXSL <smitho@bluehome.com> wrote in message news:niZtb.62808\$jy.24811@clgrps13... > Example XML: > <?xml version="1.0" encoding="UTF-8" ?>

OT
I would tell you a UDP joke, but, I am not sure you would get it... onedbguru wrote: > I would tell you a UDP joke, but, I am not sure you would get it... I'm sure that, if you did, we all will. onedbguru wrote: > I would tell you a UDP joke, but, I am not sure you would get it... See, I already did. On Sat, 24 Mar 2012 08:40:34 -0700, onedbguru wrote: > V jbhyq gryy lbh n HQC wbxr, ohg, V nz abg fher lbh jbhyq trg vg... I didn't really get the packet, can you please re-send? -- http://mgogala.byethost5.com On Saturday, March 24, 2012 2:23:42 PM UTC-4, Mladen Goga