| |

VerySource

 Forgot password?
 Register
Search
View: 10|Reply: 8

Time array sorting problem?

[Copy link]

1

Threads

4

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

Post time: 2020-3-4 15:00:02
| Show all posts |Read mode
I now have an array that stores time-formatted data. I want to arrange it in a certain order so that I can find the data I want. How can I do it?

Does anyone know how to help? I appreciate it!
Reply

Use magic Report

1

Threads

4

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-5-20 13:00:02
| Show all posts
var
  a: array [0..1024] of tdatetime;
  i: integer;
  b: tdatetime;
begin
  a [i]: = datetimepicker1.datetime; // Add multiple times

  {Sort array}

  b: = a [i]; // This is the desired result
end;
Reply

Use magic Report

0

Threads

16

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 2020-5-21 07:15:01
| Show all posts
You add the time format (FormatDateTime ('YYYY-MM-DD HH: MMMM: SS', A [I])) to a TStringList, write the sorting function factors (such as ascending, descending) according to your requirements, and then Sort with CustomSort. When taking out the data, format the string as TDateTime (B [I]: = StrToDateTime (SL [I]))
Reply

Use magic Report

0

Threads

16

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 2020-5-21 22:15:01
| Show all posts
function Compare (List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result: = CompareText (List [Index1], List [Index2]); // Ascending
end;

procedure TFormDemo.ButtonDemoClick (Sender: TObject);
var
  A: array [0..1024] of TDateTime;
  I: Integer;
  B: TDateTime;
  SL: TStringList;
begin
  Randomize;
  SL: = TStringList.Create;
  for I: = 0 to 1024 do
  begin
    A [I]: = DateUtils.IncHour (Now, Random (24));
    SL.Add (FormatDateTime ('YYYY-MM-DD HH: MMMM: SS', A [I]));
  end;
  SL.CustomSort (@Compare);
  ListBox.Items: = SL; // Look at the result
  B: = StrToDateTime (SL [5]);
end;
Reply

Use magic Report

0

Threads

16

Posts

12

Credits

Newbie

Rank: 1

Credits
12

 China

Post time: 2020-5-22 08:15:01
| Show all posts
Of course, you can also reassign A [I] in order after sorting
Reply

Use magic Report

1

Threads

4

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-5-23 09:00:01
| Show all posts
Hello, I see the results of sorting!
Let me change my code to see if it works.
In fact, what I want in the end is to use another time to find the time closest to it in this array. I think it will not be difficult to find it again after sorting.
Reply

Use magic Report

1

Threads

4

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

 Author| Post time: 2020-6-2 14:30:01
| Show all posts
Still not possible, the time data is sorted, but I can’t find the data I want!
Such:
How to find the largest number in the stringlist that is less than the specified number, you haven't used stringlist very much. !
Many thanks!
Reply

Use magic Report

0

Threads

2

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-6-3 21:30:01
| Show all posts
If you want to find the time closest to the time you give, do you need to sort first? Wouldn't it be enough to just traverse once? . Of course, if your time array is fixed, and you need to search multiple times, it is still useful to sort it first.
You can use the bubbling method to sort. But when you find time, you can use the dichotomy.
Reply

Use magic Report

0

Threads

2

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-6-3 23:30:01
| Show all posts
In addition, date and time are actually floating point numbers in memory. Floating point sorting should be easy.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list