is a vector c("B", "A", "C", "A", "B") for which (by default) the levels are set to A, B, C. The reason for this is explained in the help of the function for factor ( Parameter:levels an optional vector of the unique values (as character strings) that x might have taken. The default is the unique set of values taken by as.character(x), sorted into increasing order of x. Note that this set can be specified as smaller than sort(unique(x)).)
So when you say unique(x) it will just sort the values of the factor ignoring the levels itself.
The levels function shows the order since thats the way it is stored in you x variable.
If you would do:
temp <- c("B", "A", "C", "A", "B")
# change the ordering of the levels
x <- factor(temp, levels = sort(x = unique(temp), decreasing = TRUE))
# same as in your example
unique(x)
#will use the order i specified in levels
levels(x)
unique will stay the same as in your example and the levels function will give the values in reverse order since i defined them that way.
So if you want always to get the levels in increasing order use: