Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #29793 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2016-03-04 02:00 -0800 |
| Last post | 2016-03-06 18:41 -0300 |
| Articles | 20 on this page of 42 — 9 participants |
Back to article view | Back to comp.lang.javascript
Bubblesort jonas.thornvall@gmail.com - 2016-03-04 02:00 -0800
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 18:53 +0800
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 12:01 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:03 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 05:15 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 05:41 -0800
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:56 +0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 15:10 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:37 -0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 17:08 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:50 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 07:54 -0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-04 17:21 +0100
Re: Bubblesort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-04 19:26 +0000
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 23:05 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-05 09:15 -0800
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-08 03:43 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-08 02:41 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-08 09:10 -0800
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:34 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:46 +0100
Re: Bubblesort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-09 00:16 +0000
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-09 02:41 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-08 23:35 +0100
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-09 02:38 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-09 22:04 +0100
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-10 21:14 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-10 22:17 +0100
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-10 22:31 +0100
Re: Bubblesort Stefan Weiss <krewecherl@gmail.com> - 2016-03-10 23:25 +0100
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-11 00:16 +0100
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:59 +0800
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 17:12 +0100
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-05 02:00 +0100
Re: Bubblesort John Harris <niam@jghnorth.org.uk.invalid> - 2016-03-05 18:39 +0000
Re: Bubblesort Scott Sauyet <scott.sauyet@gmail.com> - 2016-03-05 17:40 -0800
Re: Bubblesort "Mr. Man-wai Chang" <toylet.toylet@gmail.com> - 2016-03-04 21:54 +0800
Re: Bubblesort "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-03-04 16:59 +0100
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:04 -0800
Re: Bubblesort jonas.thornvall@gmail.com - 2016-03-04 03:55 -0800
Re: Bubblesort Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-05 00:01 +0100
Re: Bubblesort Aleksandro <aleksandro@gmx.com> - 2016-03-06 18:41 -0300
Page 1 of 3 [1] 2 3 Next page →
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-04 02:00 -0800 |
| Subject | Bubblesort |
| Message-ID | <0d68ee6d-8a80-439e-ae04-17743584e2e4@googlegroups.com> |
I was thinking sorting links for each node, using a simple bubblesort, it is not that many links on each node usually. A link can't be equal to another so no "=" needed.
arr[i] contain the nodes and nodelinks[] is an array of links 1,4,2,3,0 for example.
Does it seem in principal right?
// Sort links
function sortlinks()
{
for(var i = 0; i < nodes; nodes ++ )
{
sorted = false;
while(sorted==false)
{
sorted=true;
for (var j=0;j<arr[i].nodelinks.length;j++){
if (arr[i].nodelinks[j] > arr[i].nodelinks[j + 1])
{
temp = arr[i].nodelinks[j];
arr[i].nodelinks[j] = arr[i].nodelinks[j + 1];
arr[i].nodelinks[j + 1] = temp;
sorted = false;
}
}
}
}
}
[toc] | [next] | [standalone]
| From | "Mr. Man-wai Chang" <toylet.toylet@gmail.com> |
|---|---|
| Date | 2016-03-04 18:53 +0800 |
| Message-ID | <nbbp99$fed$1@dont-email.me> |
| In reply to | #29793 |
On 3/4/2016 6:00 PM, jonas.thornvall@gmail.com wrote: > I was thinking sorting links for each node, using a simple bubblesort, it is not that many links on each node usually. A link can't be equal to another so no "=" needed. > arr[i] contain the nodes and nodelinks[] is an array of links 1,4,2,3,0 for example. > > Does it seem in principal right? Does Javascript have a function to sort arrays? -- @~@ Remain silent. Nothing from soldiers and magicians is real! / v \ Simplicity is Beauty! May the Force and farces be with you! /( _ )\ (Fedora release 23) Linux 4.4.3-300.fc23.x86_64 ^ ^ 18:42:01 up 16:51 0 users load average: 1.04 1.04 1.05 不借貸! 不詐騙! 不援交! 不打交! 不打劫! 不自殺! 請考慮綜援 (CSSA): http://www.swd.gov.hk/tc/index/site_pubsvc/page_socsecu/sub_addressesa
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-03-04 12:01 +0100 |
| Message-ID | <XnsA5C17A5C66BA1eejj99@194.109.6.166> |
| In reply to | #29794 |
"Mr. Man-wai Chang" <toylet.toylet@gmail.com> wrote on 04 Mar 2016 in comp.lang.javascript: > On 3/4/2016 6:00 PM, jonas.thornvall@gmail.com wrote: >> I was thinking sorting links for each node, using a simple bubblesort, >> it is not that many links on each node usually. A link can't be equal >> to another so no "=" needed. arr[i] contain the nodes and nodelinks[] >> is an array of links 1,4,2,3,0 for example. >> >> Does it seem in principal right? > > Does Javascript have a function to sort arrays? If the array is just sorted numerical or string-wize hexadecimal: <http://www.w3schools.com/jsref/jsref_sort.asp> Javascript has a fine sort function that can call a compare function, the called function can compare whatever you like. So bubble-sort, while a nice exercise for newbies, is probably much slower. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-04 03:03 -0800 |
| Message-ID | <d4957caf-ee18-4438-b3d1-ce2a89859a68@googlegroups.com> |
| In reply to | #29795 |
Den fredag 4 mars 2016 kl. 12:01:28 UTC+1 skrev Evertjan.: > "Mr. Man-wai Chang" <toylet.toylet@gmail.com> wrote on 04 Mar 2016 in > comp.lang.javascript: > > > On 3/4/2016 6:00 PM, jonas.thornvall@gmail.com wrote: > >> I was thinking sorting links for each node, using a simple bubblesort, > >> it is not that many links on each node usually. A link can't be equal > >> to another so no "=" needed. arr[i] contain the nodes and nodelinks[] > >> is an array of links 1,4,2,3,0 for example. > >> > >> Does it seem in principal right? > > > > Does Javascript have a function to sort arrays? > > If the array is just sorted numerical or string-wize hexadecimal: > <http://www.w3schools.com/jsref/jsref_sort.asp> > > Javascript has a fine sort function that can call a compare function, > the called function can compare whatever you like. > > So bubble-sort, while a nice exercise for newbies, is probably much slower. > > > -- > Evertjan. > The Netherlands. > (Please change the x'es to dots in my emailaddress) Thank's Evertjan.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-04 05:15 -0800 |
| Message-ID | <086a7397-459b-46e3-b83c-cfc7edf4188a@googlegroups.com> |
| In reply to | #29795 |
Den fredag 4 mars 2016 kl. 12:01:28 UTC+1 skrev Evertjan.:
> "Mr. Man-wai Chang" <toylet.toylet@gmail.com> wrote on 04 Mar 2016 in
> comp.lang.javascript:
>
> > On 3/4/2016 6:00 PM, jonas.thornvall@gmail.com wrote:
> >> I was thinking sorting links for each node, using a simple bubblesort,
> >> it is not that many links on each node usually. A link can't be equal
> >> to another so no "=" needed. arr[i] contain the nodes and nodelinks[]
> >> is an array of links 1,4,2,3,0 for example.
> >>
> >> Does it seem in principal right?
> >
> > Does Javascript have a function to sort arrays?
>
> If the array is just sorted numerical or string-wize hexadecimal:
> <http://www.w3schools.com/jsref/jsref_sort.asp>
>
> Javascript has a fine sort function that can call a compare function,
> the called function can compare whatever you like.
>
> So bubble-sort, while a nice exercise for newbies, is probably much slower.
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
I thought this would sort the property "subarray" nodelinks.
arr[0].nodelinks.sort();
But after sort it write out same content as before sort.
1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9
My object something like this.
arr[k] ={nodelinks : []};
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-04 05:41 -0800 |
| Message-ID | <7b1ee9db-4732-48e1-be7e-37ba62db4c6e@googlegroups.com> |
| In reply to | #29799 |
Den fredag 4 mars 2016 kl. 14:15:50 UTC+1 skrev jonas.t...@gmail.com:
> Den fredag 4 mars 2016 kl. 12:01:28 UTC+1 skrev Evertjan.:
> > "Mr. Man-wai Chang" <toylet.toylet@gmail.com> wrote on 04 Mar 2016 in
> > comp.lang.javascript:
> >
> > > On 3/4/2016 6:00 PM, jonas.thornvall@gmail.com wrote:
> > >> I was thinking sorting links for each node, using a simple bubblesort,
> > >> it is not that many links on each node usually. A link can't be equal
> > >> to another so no "=" needed. arr[i] contain the nodes and nodelinks[]
> > >> is an array of links 1,4,2,3,0 for example.
> > >>
> > >> Does it seem in principal right?
> > >
> > > Does Javascript have a function to sort arrays?
> >
> > If the array is just sorted numerical or string-wize hexadecimal:
> > <http://www.w3schools.com/jsref/jsref_sort.asp>
> >
> > Javascript has a fine sort function that can call a compare function,
> > the called function can compare whatever you like.
> >
> > So bubble-sort, while a nice exercise for newbies, is probably much slower.
> >
> >
> > --
> > Evertjan.
> > The Netherlands.
> > (Please change the x'es to dots in my emailaddress)
>
> I thought this would sort the property "subarray" nodelinks.
>
> arr[0].nodelinks.sort();
>
> But after sort it write out same content as before sort.
> 1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9
>
> My object something like this.
>
> arr[k] ={nodelinks : []};
I can not find any examples for howto sort an array that is a property of another array.
So if anyone or you Evertjan have the time so show me, i would be greatful.
I realise it can not be that hard, but i thought my attempt made sense, evidently it didn't. So how do i reach the array and sort it.
[toc] | [prev] | [next] | [standalone]
| From | "Mr. Man-wai Chang" <toylet.toylet@gmail.com> |
|---|---|
| Date | 2016-03-04 21:56 +0800 |
| Message-ID | <nbc417$m25$2@dont-email.me> |
| In reply to | #29801 |
On 3/4/2016 9:41 PM, jonas.thornvall@gmail.com wrote: > > I can not find any examples for howto sort an array that is a property of another array. > > So if anyone or you Evertjan have the time so show me, i would be greatful. Can you flatten the tree into a linear array, sort it, then rebuild the tree? -- @~@ Remain silent. Nothing from soldiers and magicians is real! / v \ Simplicity is Beauty! May the Force and farces be with you! /( _ )\ (Fedora release 23) Linux 4.4.3-300.fc23.x86_64 ^ ^ 21:42:01 up 19:51 0 users load average: 1.05 1.03 1.05 不借貸! 不詐騙! 不援交! 不打交! 不打劫! 不自殺! 請考慮綜援 (CSSA): http://www.swd.gov.hk/tc/index/site_pubsvc/page_socsecu/sub_addressesa
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2016-03-04 15:10 +0100 |
| Message-ID | <nbc50g$44d$1@news.albasani.net> |
| In reply to | #29801 |
On 03/04/2016 14:41, jonas.thornvall@gmail.com wrote:
>> I thought this would sort the property "subarray" nodelinks.
>>
>> arr[0].nodelinks.sort();
>>
>> But after sort it write out same content as before sort.
>> 1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9
>>
>> My object something like this.
>>
>> arr[k] ={nodelinks : []};
>
> I can not find any examples for howto sort an array that is a property of another array.
The result you describe is typically what you get when you use sort()
with string elements instead of numbers. Ideally, you should make sure
that you actually insert numbers into `arr[x].nodelinks`, not strings.
Alternatively, you can sort string array elements using a numeric
comparison function:
arr[x].nodelinks.sort((a, b) => a - b);
Or in a more verbose form (but with better UA compatibility):
function compareNumeric (a, b)
{
return a - b;
}
arr[x].nodelinks.sort(compareNumeric);
- stefan
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-04 07:37 -0800 |
| Message-ID | <f525869e-a6db-4397-9ac7-eca6c01c5ed4@googlegroups.com> |
| In reply to | #29805 |
Den fredag 4 mars 2016 kl. 15:10:31 UTC+1 skrev Stefan Weiss:
> On 03/04/2016 14:41, jonas.thornvall@gmail.com wrote:
> >> I thought this would sort the property "subarray" nodelinks.
> >>
> >> arr[0].nodelinks.sort();
> >>
> >> But after sort it write out same content as before sort.
> >> 1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9
> >>
> >> My object something like this.
> >>
> >> arr[k] ={nodelinks : []};
> >
> > I can not find any examples for howto sort an array that is a property of another array.
>
> The result you describe is typically what you get when you use sort()
> with string elements instead of numbers. Ideally, you should make sure
> that you actually insert numbers into `arr[x].nodelinks`, not strings.
>
> Alternatively, you can sort string array elements using a numeric
> comparison function:
>
> arr[x].nodelinks.sort((a, b) => a - b);
>
> Or in a more verbose form (but with better UA compatibility):
>
> function compareNumeric (a, b)
> {
> return a - b;
> }
>
> arr[x].nodelinks.sort(compareNumeric);
>
>
> - stefan
Can a counter create string values, because the values from a counter passed to the array?
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2016-03-04 17:08 +0100 |
| Message-ID | <nbcbto$hhg$1@news.albasani.net> |
| In reply to | #29807 |
On 03/04/2016 16:37, jonas.thornvall@gmail.com wrote: > Den fredag 4 mars 2016 kl. 15:10:31 UTC+1 skrev Stefan Weiss: >> On 03/04/2016 14:41, jonas.thornvall@gmail.com wrote: >>>> I thought this would sort the property "subarray" nodelinks. >>>> >>>> arr[0].nodelinks.sort(); >>>> >>>> But after sort it write out same content as before sort. >>>> 1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9 ... >> The result you describe is typically what you get when you use sort() >> with string elements instead of numbers. Ideally, you should make sure >> that you actually insert numbers into `arr[x].nodelinks`, not strings. ... > Can a counter create string values, because the values from a counter > passed to the array? That would be unusual, but I don't know what you're using as a counter. I've only seen one line of code using sort() and your list of sorted values. That the values are strings was just an educated guess. It's possible that the problem is caused by something else. - stefan
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-04 07:50 -0800 |
| Message-ID | <6f62b008-adc9-4028-9a60-f935fe2f2d83@googlegroups.com> |
| In reply to | #29805 |
Den fredag 4 mars 2016 kl. 15:10:31 UTC+1 skrev Stefan Weiss:
> On 03/04/2016 14:41, jonas.thornvall@gmail.com wrote:
> >> I thought this would sort the property "subarray" nodelinks.
> >>
> >> arr[0].nodelinks.sort();
> >>
> >> But after sort it write out same content as before sort.
> >> 1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9
> >>
> >> My object something like this.
> >>
> >> arr[k] ={nodelinks : []};
> >
> > I can not find any examples for howto sort an array that is a property of another array.
>
> The result you describe is typically what you get when you use sort()
> with string elements instead of numbers. Ideally, you should make sure
> that you actually insert numbers into `arr[x].nodelinks`, not strings.
>
> Alternatively, you can sort string array elements using a numeric
> comparison function:
>
> arr[x].nodelinks.sort((a, b) => a - b);
>
> Or in a more verbose form (but with better UA compatibility):
>
> function compareNumeric (a, b)
> {
> return a - b;
> }
>
> arr[x].nodelinks.sort(compareNumeric);
>
>
> - stefan
Script printout and break after firstsort, the array uses
<HTML><BODY onLoad=init();>
<form name="network" onsubmit="init(); return false;" onSubmit>
Links:<input name="linkval" value="8" size="7" type="text"><br>
Multiple:<input name="lmult" value="3" size="3" type="text"><br>
Nodes: <input name="nodeval" value="" size="5" type="text" disabled><br>
<input type=submit name="network" value="NEW NETWORK"><br>
<textarea name="output" rows="100" cols="60"></textarea>
</form>
</BODY></HTML>
<script type="text/javascript">
function init()
{
links = document.network.linkval.value * 1;
multiple = document.network.lmult.value * 1;
nodes = (links * multiple) + 1;
document.network.nodeval.value = nodes;
arr = new Array();
myboolean = false;
// while(myboolean == false)
// {
createarr();
myboolean = createLinks();
sortlinks();
// }
for (var z = 0; z < nodes; z ++ )
{
document.network.output.value += z + "'st NODE Links-> "+arr[z].nodelinks+"\n";
}
}
// Sort links
function sortlinks()
{
mytext="";
for(var i = 0; i < nodes-1; nodes ++ )
{
arr[i].nodelinks.sort();
document.network.output.value += i + "'st NODE Links-> "+arr[i].nodelinks+"\n";
p.stop();
}
}
function createarr()
{
for(k = 0; k < nodes; k ++ )
{
arr[k] =
{
nodelinks : [], nroflinks : 0
}
;
}
}
function createLinks()
{
document.network.output.value = "";
var i = 0;
var j = 0;
var newnode = false;
// document.network.output.value += "[***START NODE***->0 \n";
while(i < nodes - 1)
{
// if(i < nodes - 1) document.network.output.value += "LINKS TO NODE -> " + arr[i].nodelinks + "\n\n";
j = arr[i].nodelinks.length;
temparr = new Array();
while(j < links)
{
dublett = false;
// Only have to generate random values that is bigger than "i" otherwise node full / exhausted
aLink = Math.floor(Math.random() * (nodes - 1 - i)) + i + 1;
if (temparr[0] == null)
{
temparr[0] = aLink;
}
for(k = 0; k < arr[i].nodelinks.length; k ++ )
{
if(aLink == arr[i].nodelinks[k])
{
dublett = true;
}
}
// document.network.output.value += i + "]" + " Linked-->" + aLink + " " + dublett + "\n";
if(dublett == false && arr[aLink].nodelinks.length < links)
{
// document.network.output.value += "[ADDED LINK] " + aLink + "\n\n";
arr[i].nodelinks[arr[i].nodelinks.length] = aLink;
arr[aLink].nodelinks[arr[aLink].nodelinks.length] = i;
j ++ ;
newnode = true;
}
if (newnode == false)
{
inmylist = false;
for(var m = 0; m < temparr.length; m ++ )
{
if(temparr[m] == aLink)
{
inmylist = true;
// document.network.output.value += inmylist + "Nothing added to list [" + temparr + "\n";
}
}
if (inmylist == false)
{
temparr[temparr.length] = aLink;
// document.network.output.value += "Added to mylist" + temparr + "\n";
}
scope = (nodes - 1) - i;
// document.network.output.value += "Break if TRUE " + temparr.length + ">=" + scope + "\n";
if (temparr.length == scope)
{
document.network.output.value += "Hurray **failed** generate regular graph \n";
myboolean = false;
return myboolean;
}
}
newnode = false;
}
// document.network.output.value += "***FINISHED NODE*** " + i + " Links =" + arr[i].nodelinks + "\n\n";
i ++ ;
// document.network.output.value += "[***START NODE***-> " + i + " ";
}
if(arr[i].nodelinks.length < links)
{
document.network.output.value += "Hurray **failed** generate regular graph \n";
myboolean = false;
return myboolean;
}
else
{
document.network.output.value += "Hurray **succeded** to generate network!!!\n";
myboolean = true;
return myboolean;
}
}
</script>
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-04 07:54 -0800 |
| Message-ID | <26b2b860-b6c6-40ff-bcf6-64d6a9baa55e@googlegroups.com> |
| In reply to | #29805 |
Den fredag 4 mars 2016 kl. 15:10:31 UTC+1 skrev Stefan Weiss:
> On 03/04/2016 14:41, jonas.thornvall@gmail.com wrote:
> >> I thought this would sort the property "subarray" nodelinks.
> >>
> >> arr[0].nodelinks.sort();
> >>
> >> But after sort it write out same content as before sort.
> >> 1,10,11,12,13,14,15,16,17,2,3,4,5,6,7,8,9
> >>
> >> My object something like this.
> >>
> >> arr[k] ={nodelinks : []};
> >
> > I can not find any examples for howto sort an array that is a property of another array.
>
> The result you describe is typically what you get when you use sort()
> with string elements instead of numbers. Ideally, you should make sure
> that you actually insert numbers into `arr[x].nodelinks`, not strings.
>
> Alternatively, you can sort string array elements using a numeric
> comparison function:
>
> arr[x].nodelinks.sort((a, b) => a - b);
>
> Or in a more verbose form (but with better UA compatibility):
>
> function compareNumeric (a, b)
> {
> return a - b;
> }
>
> arr[x].nodelinks.sort(compareNumeric);
>
>
> - stefan
Sorry i bothered you your approach do indeed ***work***, apparently it is a string but i can't understand how random makes a string value.
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2016-03-04 17:21 +0100 |
| Message-ID | <nbcclt$j5p$1@news.albasani.net> |
| In reply to | #29809 |
On 03/04/2016 16:54, jonas.thornvall@gmail.com wrote: > apparently it is a string but i can't understand how random makes a > string value. I could take a look, but I won't. If you want help, you should: 1) declare your variables 2) post only the relevant part of your code 3) trim the quoted text down to the minimum necessary for context You've been asked to do all of the above many times, and you continue to ignore it. If you can't spare the time to format your questions in a reasonable way, I will not read or answer them. - stefan
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-03-04 19:26 +0000 |
| Message-ID | <8737s6doyh.fsf@bsb.me.uk> |
| In reply to | #29809 |
jonas.thornvall@gmail.com writes: <snip> > Sorry i bothered you your approach do indeed ***work***, apparently it > is a string but i can't understand how random makes a string value. Math.random() does not make a string but Array.prototype.sort() uses string comparison by default. It's documented. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2016-03-04 23:05 +0100 |
| Message-ID | <XnsA5C1EAD8454FEeejj99@194.109.6.166> |
| In reply to | #29816 |
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 04 Mar 2016 in
comp.lang.javascript:
> [..] Array.prototype.sort() uses
> string comparison by default. It's documented.
document.write( [12,5,3].sort() ); // 12,3,5
Indeed it does, how strange and dangerous for the unwary!
======================
document.write( [12,5,3].sort(function(a,b){return a-b;} ) ); // 3,5,12
document.write( [12,5,3].sort( (a, b) => a - b ) ); // 3,5,12
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-05 09:15 -0800 |
| Message-ID | <6b1b7449-d2b9-4576-b6dc-dd9a79db3151@googlegroups.com> |
| In reply to | #29816 |
Den fredag 4 mars 2016 kl. 20:26:42 UTC+1 skrev Ben Bacarisse: > jonas.thornvall@gmail.com writes: > <snip> > > Sorry i bothered you your approach do indeed ***work***, apparently it > > is a string but i can't understand how random makes a string value. > > Math.random() does not make a string but Array.prototype.sort() uses > string comparison by default. It's documented. > > -- > Ben. Thank's for the help Ben. Here is my latest effort, there is still global variables. But a bit more modularisation. http://jt.node365.se/nodes14.html Now you can chose all, graphs not just the Link*deep+1=Nodes. And you can search for regular graphs and sort the links.
[toc] | [prev] | [next] | [standalone]
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Date | 2016-03-08 03:43 +0100 |
| Message-ID | <nble89$bsu$1@news.albasani.net> |
| In reply to | #29816 |
On 03/04/2016 20:26, Ben Bacarisse wrote: > jonas.thornvall@gmail.com writes: >> Sorry i bothered you your approach do indeed ***work***, apparently it >> is a string but i can't understand how random makes a string value. > > Math.random() does not make a string but Array.prototype.sort() uses > string comparison by default. It's documented. You are absolutely right. I don't know how I could have missed that. Now I feel bad for writing a snarky and unhelpful reply, when it was my mistake... Jonas, I owe you one serious answer, with no critizism of your posting or coding style. - stefan
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-08 02:41 -0800 |
| Message-ID | <946e1382-6dea-4268-8779-d665d65edd5c@googlegroups.com> |
| In reply to | #29854 |
Den tisdag 8 mars 2016 kl. 03:43:29 UTC+1 skrev Stefan Weiss: > On 03/04/2016 20:26, Ben Bacarisse wrote: > > jonas.thornvall@gmail.com writes: > >> Sorry i bothered you your approach do indeed ***work***, apparently it > >> is a string but i can't understand how random makes a string value. > > > > Math.random() does not make a string but Array.prototype.sort() uses > > string comparison by default. It's documented. > > You are absolutely right. I don't know how I could have missed that. > > Now I feel bad for writing a snarky and unhelpful reply, when it was my > mistake... Jonas, I owe you one serious answer, with no critizism of > your posting or coding style. > > - stefan No problem i take newsgroups for what they are forums some skilled, some verbose, some petifull like me and some anal ;). And so should anyone. We are all different, but now and then i get some help, thank's again Ben. I do realise programming do require abit of anal mind and obsession with details. That's not necessarily true when it comes to make algorithms and throw ideas on the board, and that is why i am glad that scriplanguages and other highlevel languages exist. They allow the sloppy to perform a task without necessarily dig into the grid of details. http://jt.node365.se/nodes15.html Next i will arrange the nodes neatly so is doesn't come out as wool yarns. There is probably some set theoretical limit for how minimum entangled nodes created using x nodes and y links, but as far as it can i will unentangle/distangle the knots. I suspect it will be easier than it seem just write out the nodes in correct order. So rather than writing any fancy algorithm to write out in a certain order i will rearrange the nodes. However i probably need some better printout functions than a circle. I somehow got the idea that an hierrchical structure may be suitable. And the i will take it into 3D. I have come to understand that many graphs equivalent to geometrical 3D structures. I will also try analyse the created graphs, circumreferences, distances, girths, convexity and so on. They say the best programmers are from linguistics, but i prefer to make things/words up ;) http://jt.node365.se/nodes15.html
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-03-08 09:10 -0800 |
| Message-ID | <9a36c61a-7ef6-415c-a14a-a9a5d0ca4493@googlegroups.com> |
| In reply to | #29857 |
Den tisdag 8 mars 2016 kl. 11:41:19 UTC+1 skrev jonas.t...@gmail.com: > Den tisdag 8 mars 2016 kl. 03:43:29 UTC+1 skrev Stefan Weiss: > > On 03/04/2016 20:26, Ben Bacarisse wrote: > > > jonas.thornvall@gmail.com writes: > > >> Sorry i bothered you your approach do indeed ***work***, apparently it > > >> is a string but i can't understand how random makes a string value. > > > > > > Math.random() does not make a string but Array.prototype.sort() uses > > > string comparison by default. It's documented. > > > > You are absolutely right. I don't know how I could have missed that. > > > > Now I feel bad for writing a snarky and unhelpful reply, when it was my > > mistake... Jonas, I owe you one serious answer, with no critizism of > > your posting or coding style. > > > > - stefan > > No problem i take newsgroups for what they are forums some skilled, some verbose, some petifull like me and some anal ;). And so should anyone. > > We are all different, but now and then i get some help, thank's again Ben. > > I do realise programming do require abit of anal mind and obsession with details. > > That's not necessarily true when it comes to make algorithms and throw ideas on the board, and that is why i am glad that scriplanguages and other highlevel languages exist. They allow the sloppy to perform a task without necessarily dig into the grid of details. > > http://jt.node365.se/nodes15.html > > Next i will arrange the nodes neatly so is doesn't come out as wool yarns. There is probably some set theoretical limit for how minimum entangled nodes created using x nodes and y links, but as far as it can i will unentangle/distangle the knots. > > I suspect it will be easier than it seem just write out the nodes in correct order. So rather than writing any fancy algorithm to write out in a certain order i will rearrange the nodes. However i probably need some better printout functions than a circle. I somehow got the idea that an hierrchical structure may be suitable. And the i will take it into 3D. I have come to understand that many graphs equivalent to geometrical 3D structures. > > I will also try analyse the created graphs, circumreferences, distances, girths, convexity and so on. > > They say the best programmers are from linguistics, but i prefer to make things/words up ;) > > http://jt.node365.se/nodes15.html Maybe a Dartboard like structure would be suitable representig girth and depth of graph in 2D.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-03-08 23:34 +0100 |
| Message-ID | <291513408.Ea4uQbgaIG@PointedEars.de> |
| In reply to | #29816 |
Ben Bacarisse wrote: > jonas.thornvall@gmail.com writes: > <snip> >> Sorry i bothered you your approach do indeed ***work***, apparently it >> is a string but i can't understand how random makes a string value. > > Math.random() does not make a string but Array.prototype.sort() uses > string comparison by default. It's documented. Imprecise. String comparison *can* be the default for Array.prototype.sort(); in fact, if the /comparefn/ argument is “undefined” (including not specified), the sort order is *implementation-dependent*. Implementations need not agree on a default comparator. So specifying a comparator is *required* in *portable* code *regardless* of the type of the elements (which can be of a different type each; but see Typed Arrays). <http://www.ecma-international.org/ecma-262/6.0/index.html#sec-array.prototype.sort> -- PointedEars Zend Certified PHP Engineer <http://www.zend.com/en/yellow-pages/ZEND024953> | Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web