Forum > General

Custom sort, is this a bug?

(1/4) > >>

Thaddy:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$mode delphi}uses classes;   function ReverseSort(List:TstringList;a,b:integer):integer;  begin    Result :=  b-a;  // negative, zero or positive as per documentation  end; var  l1:TstringList; begin  l1:=Tstringlist.create;  try    l1.addstrings(['a','b', 'c','d']);    l1.customsort(@ReverseSort);  finally    writeln(l1.text);    l1.Free;  end;end.
This outputs:
--- Code: Bash  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---cdba which is obviously not correct. I am using win10 x86_64 and fpc main here. Did I make a mistake? Can't see it.
(mode does not matter, same result with objfpc)

y.ivanov:
You're comparing indexes, not string values. You may got swaps meanwhile.

Thaddy:

--- Quote from: y.ivanov on September 01, 2022, 04:00:01 pm ---You're comparing indexes, not string values. You may got swaps meanwhile.

--- End quote ---
It was my intention to use the index and not the value. Hence I do not understand the outcome.
FIFO

y.ivanov:

--- Quote from: Thaddy on September 01, 2022, 04:03:08 pm ---
--- Quote from: y.ivanov on September 01, 2022, 04:00:01 pm ---You're comparing indexes, not string values. You may got swaps meanwhile.

--- End quote ---
It was my intention to use the index and not the value. Hence I do not understand the outcome.
FIFO

--- End quote ---
Your assumption (a<b) = (List[ a ]<List[ b ]) is correct only at the top QuickSort invocation. On each recursive call it makes a swap and the assumption breaks up.

I'm surprised you get a result at all, testing your code I've got the 'stop problem'. 

Thaddy:
That is not my assumption, the assumption is that b-a returns a value either negative, zero or positive.
The content is not important, the order is.

Navigation

[0] Message Index

[#] Next page

Go to full version