Replit DB Descending Order

How do i get all the values with prefix “bal-” in descending order? I am trying to make a leaderboard.

1 Like

Hey, @Pify welcome to the forums!

Can you please provide a link to the Repl? This way it is easier for staff and members of the community to help you!

Also see this guide on how to share your code:

3 Likes

I would try using something like this:

This code is based on the assumption that you already have from replit import db in your code

for key in db.prefix("bal-"):
	try:
		print(key)
	except Exception as E:
		print(E)

Me personally I would use the write method for the .txt files and have each error stored there.

Here is the code for that.

def FILE(txt):
	file = open("MyFile.txt", "a")
	file.writable()
	file.write(str(txt) + "\n")
	file.close()


for key in db.prefix("bal-"):
	try:
		print(key)
	except Exception as E:
		FILE(E)

Why would you need a try/except block for that?

Idk I just always do that…

Well IMO putting a try/except block on everything makes it harder to debug.

Also, your naming scheme is bad. First of all, functions shouldn’t be declared in all caps. Second, try to make the function name actually tell you what the function is about.

Updated version:

def addToFile(text):
	file = open("MyFile.txt", "a")
	file.writable()
	file.write(str(txt) + "\n")
	file.close()


for key in db.prefix("bal-"):
	print(key)

Also, I have never seen anyone use .writable(). I have no idea what it does and I don’t feel like skimming through documentation rn tbh

Fair point. I’ll start doing that.

1 More thing, if there is no special reason, you should always use the with statement for opening files.

1 Like