Bubble Sorting Alarm Times using Milliseconds

I've been beating myself up for the last few days with this one. I have a list of 10 daily schedule alarms that I'd like to sort. (ie. 12:03 PM, 10:45 AM, 11:20 AM,... etc.) The user won't necessary enter them in chronological order. Therefore, my thought is to use a bubble sort. In order to do that, my thought was to attach the milliseconds using today's date of that specific time (798234234@12:03 PM). The "@" will allow me to strip the milliseconds once times are sorted and save them back to the list in their correct order. My issue is, I can't come up with a way to find the milliseconds of a specific time today. I know it's probably easy, but I've searched across the web and tried every thing I could come up with for days. What am I missing?

After all this, I've considered converting each time to minutes using military time
(hh*60)+mm. 2:27 PM: ((14X60)+27) = 840

Then bubble sort.

But I hate getting beat by a computer...LOL

Any ideas on the milliseconds?

list sorting with blocks
(There is also an extension for sorting lists)
Have a good look at the clock timer component blocks available:
http://ai2.appinventor.mit.edu/reference/components/sensors.html#Clock

@TIMAI2 Thanks for the quick reply. I've seen these already, but neither of them responds to my specific question which was how to find the milliseconds of a specific time today (other than NOW)
Thanks for any help in answering this question.

Maybe try something like this

There are two flavors of Instant:

  • time of day, and
  • datetime

If you can feed the MakeInstant block with today's year, month, date, hour, minute by text JOINs or alternatively use the Clock block that takes all the components separately (year,month, day, etc) to make an Instant, that could give you a datetime Instant,
then feed the datetime instant to the millisecond converter,

Converting a DateTime Instant to milliseconds gives you milliseconds from 1970, an enormous number with a fixed number of digits for the foreseeable future.
That makes a nice TinyDB tag. TinyDB returns its tag lists in alphabetic order, so those datetime millisecond values will come back temporaly sorted.

Sorry you couldn't see the answer provided by my last link, maybe the ideas presented by the others will help you see what was there.

@dora_paz @ABG @TIMAI2 Ah ha! I knew there had to be an easy way. Thanks Dora! I just wasn't using military time. Abraham, thanks for the explanation. Tim, maybe I just wasn't seeing clearly enough to find the answer in what you sent me.

Can anything similar be done with AM/PM formatted time since that part of the program is already written? I wish there was a place to go to learn more details possibly with examples. I purchased 2 books but neither gets into detail like I needed. I've written many programs over the years (Retired after 40 yrs of computer teaching) learning a handful of languages over those years. That's why I say I hate to get beat by a computer, so I sure appreciate you saving me precious time.

While thinking about how to proceed, a light came on, a bell rang, or whatever. If I use military time in my list in the 1st place, there'd be no need for millis because military time can be sorted just as it is. Duh! It's like I always taught my students: Using computers, there always multiple ways of doing something. I will teach you one or two ways but that doesn't mean my way is the perfect or correct way.

Once @dora_paz showed me the solution, I decided to make it easy and create 2 lists: AlarmList containing the AM/PM alarms and the graphic above shows the creation of the MillisecondsList. Once both lists were done, it was easy to bubble sort the milliseconds and I just swapped the alarm times when the millis swapped. It works like a charm!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.