Recent

Author Topic: Bubble Sort for string and integers - Problem with conversion to ascii  (Read 10522 times)

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #15 on: April 26, 2015, 05:07:52 pm »
http://www.freepascal.org/docs-html/rtl/sysutils/trystrtoint.html

 and

http://www.freepascal.org/docs-html/rtl/sysutils/uppercase.html will come in useful.

EDIT: I should have mentioned if your comparing a string with an integer then the integer should be placed before the string.
« Last Edit: April 26, 2015, 05:13:42 pm by wildfire »
A halo is a mere circle, when does it end?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #16 on: April 26, 2015, 05:15:56 pm »
Here a ready-to-use example, based on the wiki:

Code: [Select]
procedure BubbleSort( a: TStrings );
var
  n, newn, i:integer;
begin
  n := a.Count - 1;
  repeat
    newn := 0;
    for i := 1 to n   do
      begin
        if a[ i - 1 ] > a[ i ] then
          begin
            a.Exchange( i - 1 ,  i );
            newn := i ;
          end;
      end ;
    n := newn;
  until n = 0;
end;       

Code: [Select]
procedure TForm1.Button1Click(Sender :TObject);
begin
  BubbleSort(memo1.Lines); 
end;

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #17 on: April 26, 2015, 05:19:59 pm »
Typo, I get the impression that OP wants to sort a mix of integer and strings, your examples only sort one or the other.
A halo is a mere circle, when does it end?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #18 on: April 26, 2015, 05:22:42 pm »
Memo lines are always strings.

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #19 on: April 26, 2015, 05:28:21 pm »
Yes but strings don't compare number values correctly.

eg 2 is < 123 but '2' is > '123'
A halo is a mere circle, when does it end?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #20 on: April 26, 2015, 05:33:20 pm »
You need a more elaborate solution to accomplish this.

Maybe you can try it.

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #21 on: April 26, 2015, 05:36:42 pm »
It's simple enough...

Using TryStrToInt on the items you want to compare... If both items return true then compare the integers
If one is true and the other is false then place the item that returns true above the one that returns false.
If both return false then compare the UpperCase values of both strings.
A halo is a mere circle, when does it end?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #22 on: April 26, 2015, 05:39:27 pm »
And if the integers are inside the strings, like this?

Code: [Select]
abc22def
abc2def

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Bubble Sort for string and integers - Problem with conversion to ascii
« Reply #23 on: April 26, 2015, 05:40:24 pm »
I see your point :)
A halo is a mere circle, when does it end?

 

TinyPortal © 2005-2018