線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2119
推到 Plurk!
推到 Facebook!

陣列排序問題一問 -- 已爬文

尚未結案
alfarene
一般會員


發表:6
回覆:7
積分:2
註冊:2004-10-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-09 19:07:20 IP:211.72.xxx.xxx 未訂閱
 procedure TFormFixture.LoadTestingPlan_Standalone;
var
   CheckPlanFile : Tinifile;
   CheckPlanSection, CheckPlanFieldName, CheckPlanFieldValue : Tstrings;
   I, J: integer;
   TestingOrder : array of integer;
   TempTestingOrder, TempTestingItemNo, TempItemDesc, TempParameter1, Parameter2, Parameter3, TempErrorCode, TempContinueIfFail, TempUsed : string;
begin
     //Load Check Plan File
     if FileExists(ExtractFilePath(Application.exename)   G_Model   '_CheckPlan.ini') then
        begin
           CheckPlanSection := TStringList.Create;
           try
             CheckPlanFile := Tinifile.Create(ExtractFilePath(Application.exename)   G_Model   '_CheckPlan.ini');
             CheckPlanFile.ReadSections(CheckPlanSection);
             SetLength(TempPlanList, CheckPlanSection.Count);
             SetLength(CheckPlanList, CheckPlanSection.Count);           
           finally
             for J := 0 to CheckPlanSection.Count - 1 do
                begin
                 TempPlanList[J].TestingOrder  := CheckPlanFile.ReadString(CheckPlanSection[J],'TestingOrder','');
                 TempPlanList[J].TestingItemNo := StrToInt(CheckPlanSection[J]);
                 TempPlanList[J].ItemDesc      := CheckPlanFile.ReadString(CheckPlanSection[J],'ItemDesc','');
                 TempPlanList[J].Parameter1    := CheckPlanFile.ReadString(CheckPlanSection[J],'Parameter','');
                 TempPlanList[J].ErrorCode     := CheckPlanFile.ReadString(CheckPlanSection[J],'ErrorCode','');
                 TempPlanList[J].ContinueIfFail :=  Not (Uppercase(CheckPlanFile.ReadString(CheckPlanSection[J],'StopIfFail','')) = 'Y');
                 TempPlanList[J].Used          := 'Y';                
               end;                         //Sorting
             for J := CheckPlanSection.Count - 1 downto 0 do
               begin
                 for I := 0 to J do
                   begin
                     if StrToInt(TempPlanList[I 1].TestingOrder) > StrToInt(TempPlanList[I].TestingOrder) then
                       begin
                         TempTestingOrder := TempPlanList[I].TestingOrder;
                         TempPlanList[I].TestingOrder := TempPlanList[I 1].TestingOrder;
                         TempPlanList[I 1].TestingOrder := TempTestingOrder;
                       end
                   end
               end;            //Assign to CheckPlanList
            for I := 0 to CheckPlanSection.Count - 1 do
                 if Uppercase(CheckPlanFile.ReadString(CheckPlanSection[i],'Enabled','')) = 'Y' then
                   begin
                     CheckPlanList[I].ItemNo        := IntToStr(I 1);
                     CheckPlanList[I].TestingItemNo := TempPlanList[J].TestingItemNo;
                     CheckPlanList[I].ItemDesc      := TempPlanList[J].ItemDesc;
                     CheckPlanList[I].Parameter    := TempPlanList[J].Parameter;
                     CheckPlanList[I].ErrorCode     := TempPlanList[J].ErrorCode;
                     CheckPlanList[I].ContinueIfFail := TempPlanList[J].ContinueIfFail;
                     CheckPlanList[I].Used          := TempPlanList[J].Used;
                   end;               end;
        end
以上是source code,程式是將INI檔讀入然後放入陣列中,再根據其中一欄(TestingOrder)做排序. 最後根據排序的結果,再將所有資料照順序一一放入另一個陣列中. 但是排序做到一半就會出現'Access violation'的錯誤訊息. 此外,這段程式只是個人目前的想法,還不知道最後是否會根據TestingOrder排序的結果放入另一個陣列. 煩請各位高手為在下解答.謝謝!!
ha0009
版主


發表:16
回覆:507
積分:639
註冊:2002-03-16

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-10 09:03:59 IP:211.76.xxx.xxx 未訂閱
你好:
    你的問題可以善用 TStringList 來解決,希望下面的範例可以幫你 ^^    // 自訂的比較函式
function Compare (List: TStringList; Index1, Index2: Integer): Integer;
begin
  result := CompareStr (List.Strings [index1], List.Strings [index2]);
end;    procedure TForm1.FormCreate(Sender: TObject);
var
  s : TStringList;
begin
  s := TStringList.Create;
  s.LoadFromFile('c:\w3ctrs.ini');
  try
    s.CustomSort(Compare);
  finally
    FreeAndNil (s);
  end;
end;    解析:
    TStringsList 有一個 CustomSort 程序是讓設計者傳入比對的自訂程序,
    就是範例中的 Compare, TStringList 並使用快速排序法進行排序。
alfarene
一般會員


發表:6
回覆:7
積分:2
註冊:2004-10-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-10 16:34:25 IP:211.72.xxx.xxx 未訂閱
謝謝您的回應,可是因為我是想根據其中的一個欄位作排序(TestingOrder). 但是似乎TempPlanList[J].TestingOrder.CustomSort(Compare); 並不能這樣用. 小弟對程設涉入未深,問題程度短淺,尚請包含,謝謝!!
ha0009
版主


發表:16
回覆:507
積分:639
註冊:2002-03-16

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-03-11 01:40:35 IP:61.56.xxx.xxx 未訂閱
你好:
  你確定不行這樣用嗎? ini 檔有一定的格式,只要先把欄位與值拆解開來,再
依你的需求在進行比較。怎會不行呢? 雖說方法不一定最好,但卻是真正可行的喔。
alfarene
一般會員


發表:6
回覆:7
積分:2
註冊:2004-10-14

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-03-15 16:11:31 IP:59.120.xxx.xxx 未訂閱
您好,已用別的方法解決了,還是謝謝您喔!!
系統時間:2024-05-06 16:44:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!