15,1,5,10,13,14,50,4,55,8,67,68,69,3,12,57,70,74 字符串,排序后再把连续数字用-连接在一起,结果为:1,3-5,8,10,12-15,50,55,57,67-70,74
- function NumberSort(List: TStringList; Index1, Index2: Integer): Integer;
- var
- Value1,Value2:Integer;
- begin
- Value1:=StrToInt(List[Index1]);
- Value2:=StrToInt(List[Index2]);
- if Value1<Value2 then
- Result:=-1
- else if Value1>Value2 then
- Result:=1
- else
- Result:=0;
- end;
- procedure TForm1.btn3Click(Sender: TObject);
- var
- strTemp:string;
- strs:TStringList;
- i,j:integer;
- begin
- strTemp:='15,1,5,10,13,14,50,4,55,8,67,68,69,3,12,57,70,74';
- strs:=TStringList.Create;
- strs.Delimiter:=',';
- strs.DelimitedText:=strTemp;
- strs.CustomSort(NumberSort);
- i:=1;
- strTemp:=strs[0];
- while true do
- begin
- if i>=strs.Count then
- break;
- if StrToInt(strs[i])-StrToInt(strs[i-1])=1 then
- begin
- for j:=i to strs.Count-2 do
- if StrToInt(strs[j+1])-StrToInt(strs[j])=1 then
- continue
- else
- break;
- i:=j;
- strTemp:=strTemp+'-'+strs[i];
- end else
- strTemp:=strTemp+','+strs[i];
- inc(i);
- end;
- Memo1.Lines.Text:=strTemp;
- strs.Free;
- end;